IEnumerable类型依赖属性无法正常工作的问题求助
IEnumerable类型依赖属性无法正常工作的问题求助
我做了一个这样的用户控件:
<ItemsControl x:Name="_opt" ItemsSource="{Binding Sources, RelativeSource={RelativeSource AncestorType=UserControl}}"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding }"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
接着我定义了一个IEnumerable类型的依赖属性,代码大概是这样的:
public IEnumerable Sources { get { return (IEnumerable)GetValue(SourcesProperty); } set { SetValue(SourcesProperty, value); } } public static readonly DependencyProperty SourcesProperty = DependencyProperty.Register("Sources", typeof(IEnumerable), typeof(MyUserControl), new PropertyMetadata(null));
我的用户控件构造函数很简单,就是初始化组件:
public MyUserControl() { InitializeComponent(); }
现在遇到了一个头疼的问题:当我把字符串列表赋值给Sources属性的时候,界面上完全不显示任何内容,但如果直接把这个字符串列表赋值给ItemsControl的ItemsSource,就能正常显示。我在用户控件内部设置列表是没问题的,但在外部使用这个用户控件的时候,通过Sources属性赋值就不管用,不管怎么试都没法更新显示内容。
我是这样使用这个用户控件的:
<local:MyUserControl x:Name="_uc"/>
public NewControl() { InitializeComponent(); _uc.Sources = MyListofString; }
我感觉依赖属性的定义是正确的,但就是搞不懂为啥绑定或者赋值之后不更新显示,有没有大佬能帮我排查下问题出在哪?
备注:内容来源于stack exchange,提问作者prabhaat
相关产品推荐
相关产品推荐

