使用ItemSsource属性创建用户控件

时间:2013-07-18 20:22:29

标签: c# wpf xaml binding listbox

我有UserControl作为其中一个孩子的ListBox。我希望能够在使用控件时在ItemsSource本身上定义UserControl依赖项属性,并让孩子ListBox将此值用作自己的ItemsSource属性。

我在IEnumerable的代码隐藏中定义了ObservableCollection类型的依赖项属性(我也尝试使用UserControl),但绑定似乎只适用一次 - 当源列表更改后,CollectionChanged通知似乎没有通过UserControl及其子ListBox传播。

如何在我的UserControl上定义一个属性,该属性会更新ListBox,就好像ListBox中没有嵌入UserControl一样?

这是我的UserControl的简化视图:

ItemsSource属性的定义:

public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(SidePanelItem), new FrameworkPropertyMetadata(null));

public IEnumerable ItemsSource
{
    get { return (IEnumerable)GetValue(ItemsSourceProperty); }
    set { base.SetValue(ItemsSourceProperty, value); }
}

尝试将此属性用作ListBox的{​​{1}}:

ItemSource

0 个答案:

没有答案