我有UserControl一个ListBox作为它的孩子之一。我希望能够在使用控件时ItemsSource在自身上定义一个依赖属性UserControl,并让孩子ListBox将此值用作自己的ItemsSource属性。
我在我的代码隐藏中定义了一个类型的依赖属性IEnumerable(我也尝试过使用ObservableCollection)UserControl,但绑定似乎只适用一次 - 当源列表更改时,CollectionChanged通知似乎不会通过UserControland 传播到它的孩子ListBox。
如何在 my 上定义一个属性,该属性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's ItemSource:
<UserControl x:Name="myControl">
<.. other elements ..>
<ListBox ItemsSource="{Binding ElementName=myControl, Path=ItemsSource}"/>
</.. other elements ..>
</UserControl>