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 | Show 4 more comments1 Answer
Reset to default 0I resolved it by using ObservableCollection
instead of List
.
Still not sure why List was not working.
<ItemsControl ItemsSource="{Binding Sources, RelativeSource={RelativeSource AncestorType=UserControl}}">
– Clemens Commented Nov 20, 2024 at 8:27_uc.Sources = MyListofString;
. If elements are added later, MyListofString should be an ObservableCollection. – Clemens Commented Nov 20, 2024 at 10:01