向着光-
2021-07-30 21:38
采纳率: 100%
浏览 124

WPF datagrid中下拉框ItemsSource刷新后,已经选择的文本丢失

img

img

img

如图,第一行选择了配方之后,第二行再选择配方的时候需要更新下拉框,下拉框更新后却把第一行已经选择的数据给清除了。后台是将下拉框的ObservableCollection集合clear之后重新赋值刷新下拉框。如何解决?

                    <DataGridTemplateColumn Header="钻井液配方" HeaderStyle="{StaticResource columnHeaderCenter}" Width="200">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox  IsEditable="True" 
                                           Text="{Binding Composition,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                           ItemsSource="{Binding DataContext.Composition_list,Mode=Default, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" >
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="DropDownOpened">
                                            <command:EventToCommand CommandParameter="{Binding Id}"  Command="{Binding DataContext.DropDownCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}"/>
                                        </i:EventTrigger>
                                        <i:EventTrigger EventName="DropDownClosed">
                                            <command:EventToCommand CommandParameter="{Binding Id}"  Command="{Binding DataContext.SelectionChangedCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}"/>
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </ComboBox>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>


        //刷新下拉框数据
        public void Refresh_list(int id)
        {
            var fluidMaintainanceDatas = DataBase.GetFluidMaintainanceDatas();
            var performanceDatas=DataBase.GetDrillingFluidPerformanceDatas();
            var system = performanceDatas[id].DrillingFluidSystem;
            var temp = fluidMaintainanceDatas.Where(t => t.DrillingFluidSystem == system);
            var compositionDatas = temp.Select(t => t.Composition);
            Composition_list.Clear();
            foreach (var compositionData in compositionDatas)
            {
                Composition_list.Add(compositionData);
            }
        }

  • 点赞
  • 收藏

1条回答 默认 最新

  • 已采纳

    没用过wpf,不过大概看了你的代码,下面加粗的是共用的吧,所以导致了清空后所有combobox都重新绑定了新的数据源。你看下api是否有获取当前combobox对象的方法,获取到combobox后,重新生成一个数据源来绑定,而不是共用一个。

                                <ComboBox  IsEditable="True" 
                                           Text="{Binding Composition,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                           ItemsSource="{Binding **DataContext.Composition_list**,Mode=Default, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" >
    
    点赞 打赏 评论

相关推荐 更多相似问题