Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set the initial states of selected items in CheckComboBox

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.

like image 570
Jatin Avatar asked Apr 03 '15 07:04

Jatin


1 Answers

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

like image 152
Ron Avatar answered Nov 10 '22 05:11

Ron