Search code examples
c#wpfxceed

Checkcombobox item is not checked


I have problem with xceed CheckComboBox.

Lets say I have this code:

<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>

The combobox is filled normally. But selected items, which I load from XML ,are not checked. Why?

I load XML like this:

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>();
}    

I store XML file in a database from which I read it.


Solution

  • That happens because you use complex object as item source and object equality comes in to play. Even if your objects have all the same properties they have different references. So they are treated as different object. You can do either find items from original source that matches your selected list and make your selection list from them or override your models Equal method with a logic to make it work. (eg: if ids are equal then objects are equal too) Example for the first approach :

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