最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - IEnumerable dependency property not working as expected - Stack Overflow

programmeradmin0浏览0评论

I have a user Control that looks like this,

<ItemsControl x:Name="_opt" ItemsSource="{Binding Sources, RelativeSource={RelativeSource AncestorType=UserControl}}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding }"/>           
        </DataTemplate>
     </ItemsControl.ItemTemplate>
 </ItemsControl>

I have created a IEnumerable dependency property called sources somewhat like this,

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));

My usercontrol constructor looks like this, I'm assigning Itemsource here too,

public MyUserControl()
{
    InitializeComponent();
}

The problem that I'm facing is when I assign a list of string to Sources property it shows nothing, but If i set the Itemsource directly to that list of string, I can do that. Now I can set list of string in the usercontrol but if I'm using usercontrol somewhere, there I can set the Itemsource and that the reason I created an IEnumerable.

This is how I'm using my Usercontrol,

<local:MyUserControl x:Name="_uc"/>

public NewControl()
{
    InitializeComponent();
    _uc.Sources = MyListofString;
}

No matter what I try, whenever I bind or assign the list, it doesn't show any item. I'm I made the property correctly still can't wrap my head around onto why this not updating.

I have a user Control that looks like this,

<ItemsControl x:Name="_opt" ItemsSource="{Binding Sources, RelativeSource={RelativeSource AncestorType=UserControl}}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding }"/>           
        </DataTemplate>
     </ItemsControl.ItemTemplate>
 </ItemsControl>

I have created a IEnumerable dependency property called sources somewhat like this,

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));

My usercontrol constructor looks like this, I'm assigning Itemsource here too,

public MyUserControl()
{
    InitializeComponent();
}

The problem that I'm facing is when I assign a list of string to Sources property it shows nothing, but If i set the Itemsource directly to that list of string, I can do that. Now I can set list of string in the usercontrol but if I'm using usercontrol somewhere, there I can set the Itemsource and that the reason I created an IEnumerable.

This is how I'm using my Usercontrol,

<local:MyUserControl x:Name="_uc"/>

public NewControl()
{
    InitializeComponent();
    _uc.Sources = MyListofString;
}

No matter what I try, whenever I bind or assign the list, it doesn't show any item. I'm I made the property correctly still can't wrap my head around onto why this not updating.

Share Improve this question edited Nov 20, 2024 at 10:38 Clemens 128k13 gold badges159 silver badges286 bronze badges asked Nov 20, 2024 at 8:14 prabhaatprabhaat 618 bronze badges 9
  • 2 The Sources property has not yet been set in the MyUserControl constructor. You have to register a PropertyChangedCallback. Alternatively, bind the property like <ItemsControl ItemsSource="{Binding Sources, RelativeSource={RelativeSource AncestorType=UserControl}}"> – Clemens Commented Nov 20, 2024 at 8:27
  • I tried this, but it didn't work. – prabhaat Commented Nov 20, 2024 at 9:25
  • 1 It is also unclear whether MyListofString already contains some elements when you assign _uc.Sources = MyListofString;. If elements are added later, MyListofString should be an ObservableCollection. – Clemens Commented Nov 20, 2024 at 10:01
  • It Contains elements, it is more like List<string> Mylist = new List<string>(){"a","b","c"}; – prabhaat Commented Nov 20, 2024 at 10:15
  • yes, I'm sorry, I fot to edit that part – prabhaat Commented Nov 20, 2024 at 10:33
 |  Show 4 more comments

1 Answer 1

Reset to default 0

I resolved it by using ObservableCollection instead of List. Still not sure why List was not working.

发布评论

评论列表(0)

  1. 暂无评论