Checkcombobox项未选中

我对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文件存储在我从中读取的数据库中。

esh2007 回答:Checkcombobox项未选中

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

foreach (LoadingCenter center1 in selectedLoadingCentersXml)
{
   var originalItem = ListOfCostCenters.FirstOrDefault(t=> t.Id == center1.Id);
    selectedLoadingCenters.Add(originalItem);
}
本文链接:https://www.f2er.com/3104234.html

大家都在问