Checkcombobox 项目未被选中

Checkcombobox item is not checked

我有问题 xceed CheckComboBox

假设我有这个代码:

<xceed:CheckComboBox Grid.Row="0" Grid.Column="1" Margin="2"
                 ItemsSource="{Binding Path=ListOfCostCenters}"
                 DisplayMemberPath="LoadingCenterCode"
                 SelectedItemsOverride="{Binding Path=SelectedCostCenters,
                 UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<!--<i:Interaction.Triggers>
        <i:EventTrigger EventName="ItemSelectionChanged">
            <i:InvokeCommandAction Command="{Binding Path=CompareSelectionCmd}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>-->                    
</xceed:CheckComboBox>

组合框已正常填充。但是我从 XML 加载的选定项目没有被选中。为什么?

我这样加载 XML:

List<LoadingCenter> selectedLoadingCentersXml = _moduleConfig.GetConfig<UserConfig>().LoadingCenters;

//We need to get the same Object which is in ItemSource (CostCenters) of checkBox component.
foreach (LoadingCenter center1 in selectedLoadingCentersXml)
{
    selectedLoadingCenters.Add(center1);
}

if (selectedLoadingCenters.Count > 0)
{
    //Fill the property with list of objects from CostCenters which are the same with objects from loaded XML file.
    SelectedCostCenters = new ObservableCollection<LoadingCenter>(selectedLoadingCenters);                        
}
else if (selectedLoadingCenters.Count == 0)
{
    SelectedCostCenters = new ObservableCollection<LoadingCenter>();
}    

我将 XML 文件存储在我从中读取的数据库中。

发生这种情况是因为您使用复杂对象作为项目源并且对象相等性发挥作用。即使您的对象具有所有相同的属性,它们也有不同的引用。因此,它们被视为不同的对象。您可以从原始来源中找到与您选择的列表相匹配的项目并从中创建您的选择列表,或者使用逻辑覆盖您的模型 Equal 方法以使其工作。 (例如:如果 id 相等则对象也相等) 第一种方法的示例:

foreach (LoadingCenter center1 in selectedLoadingCentersXml)
{
   var originalItem = ListOfCostCenters.FirstOrDefault(t=> t.Id == center1.Id);
    selectedLoadingCenters.Add(originalItem);
}