首页 > 解决方案 > 组合框列表更改时属性值更改为空(WPF)

问题描述

我根据条件将两个不同的列表绑定到单个组合框。

我的第一个列表包含 1,2,3,4,5,6,我的第二个列表包含 2,5,6,我根据条件从后面的代码中通过 Itemsource 将这些列表添加到组合框。

我有行网格(AllUsers 和 SomeUsers 相关数据),当我单击 AllUsers 数据行然后在网格行选择更改时,我将第一个列表从后面的代码绑定到组合框。当我单击 SomeUsers 数据行然后在网格行选择更改时,我将第二个列表从后面的代码添加到组合框。

在这里,我对网格数据和组合框使用了 observable 集合(它是 observable 集合的子列表)。在组合框中选择的任何数据都会自动更新到我的网格模型(我在组合框中使用两种方式绑定)。

Problem :- when selection changed happens I was adding relevant list to combobox through Item source from code behind , at that time previously selected value is getting null in the model property(especailly 1st list items when property contains 1 or 3 or 4 values) because那些在第二个组合框列表中不存在。

注意:- 组合框在网格之外。

架构:-不是 MVVM

<ComboBox Name="cbNum" Margin="3" Background="White" SelectedValue="{Binding UserGroup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="28"  FontSize="13" Grid.Column="1" DisplayMemberPath="Meaning" SelectedValuePath="LookupCode" VerticalContentAlignment="Center" BorderBrush="#b3b3b3"></ComboBox>
if (strCondition == "1stList")
                        cbNum.ItemsSource = lstFirtstList;
                    if (strCondition == "2ndList")
                        cbNum.ItemsSource = lstsecondList;

组合框将根据更改的网格选择而更改

标签: c#wpfcombobox

解决方案


推荐阅读