Unable to set the initial states of selected items in CheckComboBox
Asked Answered
I

2

10

I am trying to use Xceed CheckComboBox and it works well except for a small issue. When the CheckComboBox is initially loaded, the selected items List is shown properly in the ToggleButton part of the CheckComboBox, but the checkboxes representating these items aren't checked. Here is the code that I am using

XAML

<xctk:CheckComboBox x:Name="_combo"  Grid.Row="2" Grid.Column="1" 
                 ItemsSource="{Binding RoomFacilities}" 
                 HorizontalAlignment="Center" 
                 VerticalAlignment="Center"
                 DisplayMemberPath="FacilityName" 
                 SelectedItemsOverride="{Binding SelectedFaclities}" 
                    />

View Model

public class RoomBandUpdateViewModel : Screen, IHandle<RecordChanged<RoomFacility>>,
                                               IHandle<RecordDeleted<RoomFacility>> {
    private ObservableCollection<RoomFacility> _roomFacilities;
    public ObservableCollection<RoomFacility> RoomFacilities {
        get { return _roomFacilities; }
        set { _roomFacilities = value; NotifyOfPropertyChange(() => RoomFacilities); }
    }

    private ObservableCollection<RoomFacility> _selectedFacilities;
    public ObservableCollection<RoomFacility> SelectedFaclities {
        get { return _selectedFacilities; }
        set { _selectedFacilities = value; NotifyOfPropertyChange(() => SelectedFaclities); }
    }

    protected override void OnActivate() {
        SelectedFaclities = new ObservableCollection<RoomFacility>(RoomBand.Facilities);
        RoomFacilities = new ObservableCollection<RoomFacility>(facilityService.GetAll());
    }
}

I would like to know why, when SelectedFacilities are set properly in view model, the CheckComboBox's checkboxes are not checked according to the items in the SelectedFacilities. The interesting part is that the Toggle Button part of the CheckComboBox properly displays SelectedFacilities in comma separated list.

Inconsistent answered 3/4, 2015 at 7:48 Comment(1)
Did you ever solve this? I'm having the same issueFerrotype
A
2

It is not clear how you fill SelectedFacilities and RoomFacilities. Make sure you have handled reference equality of RoomFacility correctly. In other words, the only way i could only reproduce the problem is when I fill the ObservableCollections in the following pattern:

RoomFacilities m1 = new RoomFacilities() { FacilityName = "F1" };
RoomFacilities m2 = new RoomFacilities() { FacilityName = "F2" };
RoomFacilities m3 = new RoomFacilities() { FacilityName = "F3" };
RoomFacilities m4 = new RoomFacilities() { FacilityName = "F1"};
RoomFacilities m5 = new RoomFacilities() { FacilityName = "F2"};
RoomFacilities = new ObservableCollection<RoomFacilities>(new Collection<RoomFacilities>() { m1, m2, m3 });
        SelectedFacilities = new ObservableCollection<RoomFacilities>(new Collection<RoomFacilities>() { m4, m5 });

Note that although m4 and m5 have similar FacilityName (which is displayed in the ToggleButton of CheckComboBox, they will not be selected in the drop down panel, Unless you override the Equals method:

public override bool Equals(object obj)
{
    if (obj is RoomFacilities)
    {
        RoomFacilities mod = (RoomFacilities)obj;
        if (mod.FacilityName == this.FacilityName)
            return true;
        else
            return false;
    }
    return base.Equals(obj);
}
public override int GetHashCode()
{
    return FacilityName.GetHashCode();
}

In other words, CheckComboBox does not throw an exception if SelectedItemsOverride is NOT a subset of ItemsSource

Athletics answered 19/2, 2017 at 7:53 Comment(0)
S
0

If you instantiate to collection into the constructor of your class, the property is not yet bound tu your component, so the PropertyChanged event of your ViewModel is not triggered by your component.

The observable collection Add() method didn't fire the PropertyChanged event but fire the CollectionChanged event.

Hope this help you

Snowberry answered 23/5, 2016 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.