10

我正在尝试使用Xceed CheckComboBox,它运行良好,除了一个小问题。最初加载 CheckComboBox 时,所选项目列表正确显示在 CheckComboBox 的 ToggleButton 部分,但表示这些项目的复选框未选中。这是我正在使用的代码

XAML

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

查看模型

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

我想知道为什么在视图模型中正确设置SelectedFacilities时,不会根据SelectedFacilities中的项目检查 CheckComboBox 的复选框。有趣的部分是 CheckComboBox 的 Toggle Button 部分在逗号分隔列表中正确显示SelectedFacilities 。

4

2 回答 2

2

目前尚不清楚您如何填写SelectedFacilitiesRoomFacilities。确保您已正确处理了引用相等性RoomFacility。换句话说,我只能重现该问题的唯一方法是当我ObservableCollection按以下模式填充 s 时:

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

请注意,虽然m4m5有相似FacilityName(显示在ToggleButtonof 中CheckComboBox,但它们不会在下拉面板中被选中,除非您覆盖该Equals方法:

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

换句话说,CheckComboBox如果SelectedItemsOverride不是ItemsSource

于 2017-02-19T07:53:45.120 回答
0

如果您将集合实例化到类的构造函数中,则该属性尚未绑定到您的组件,因此您的 ViewModel 的 PropertyChanged 事件不会由您的组件触发。

可观察集合 Add() 方法没有触发 PropertyChanged 事件,而是触发了 CollectionChanged 事件。

希望这对你有帮助

于 2016-05-23T14:25:18.860 回答