DependencyPropety在ReactiveList.Add()上调用PropertyChangedCallback

时间:2017-02-02 15:26:57

标签: c# wpf

我根据this文章创建了自己的ComboBox

public partial class MultiSelectComboBox : UserControl
{

    public static readonly DependencyProperty ItemsSourceProperty =
         DependencyProperty.Register("ItemsSource",
             typeof(IList),
             typeof(MultiSelectComboBox),
             new FrameworkPropertyMetadata(null,
                 new PropertyChangedCallback(MultiSelectComboBox.OnItemsSourceChanged)));

    public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.Register("SelectedItems",
            typeof(IList),
            typeof(MultiSelectComboBox),
            new FrameworkPropertyMetadata(null,
                new PropertyChangedCallback(MultiSelectComboBox.OnSelectedItemsChanged)));

    public IList ItemsSource
    {
        get { return (IList)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public IList SelectedItems
    {
        get { return (IList)GetValue(SelectedItemsProperty); }
        set { SetValue(SelectedItemsProperty, value); }
    }

    private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ...
    }

    private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ...
    }
}

并在我的一个观点中使用:

<CustomControls:MultiSelectComboBox x:Name="Versions"
                ItemsSource="{Binding Versions}"
                SelectedItems="{Binding SelectedVersions}">

我绑定的属性是ReactiveUI ReactiveList<t>

    private IReactiveList<IVersion> selectedVersions;
    private IReactiveList<IVersion> versions;

    public IReactiveList<IVersion> Versions
    {
        get { return this.versions; }
        set { this.RaiseAndSetIfChanged(ref this.versions, value); }
    }

    public IReactiveList<IVersion> SelectedVersions
    {
        get { return this.selectedVersions; }
        set { this.RaiseAndSetIfChanged(ref this.selectedVersions, value); }
    }

因此,在我添加一些元素到此列表后,它不会刷新它的内容(它在我分配new ReactiveList时有效),我明白了,这是因为我和#39;我没有在ObservableCollection<T>中使用MultiSelectComboBox作为我的DP的类型但是在将IList更改为ObservableCollection<Object>后,我获得了转换异常(无法转换{{1} {}} {}} {}} {}} {}}我想要使用IReactiveList<IVersion>,因为我想在其他项目中使用它来绑定除ObservableCollection<Object>以外的其他类型。有没有什么好方法可以捕获添加/删除/清除事件?

此外,我可以说ObservableCollection<IVersion>之后我试图致电IVersion并且它没有成功。

0 个答案:

没有答案