无法在CheckComboBox中设置所选项的初始状态

时间:2015-04-03 07:48:07

标签: wpf xceed wpf-extended-toolkit

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

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

2 个答案:

答案 0 :(得分:1)

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

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(显示在ToggleButton的{​​{1}}中,但它们不会在投放中被选中向下面板,除非您覆盖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不会抛出异常

答案 1 :(得分:0)

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

可观察集合Add()方法未触发PropertyChanged事件但触发了CollectionChanged事件。

希望这能帮到你