Reputation: 8708
I have ~30 controls, each with the same model. I want, instead of having ~30 properties bound and ~30 private variables, to bind to an array, allowing me also to loop on the properties
For examle,
Say I have the following (It's just an example)
public class MyImage
{
public String source { get; set;}
public String tooltip { get; set;}
}
xaml
<Grid>
<Image Name="image0" Source="{Binding MyImage0.source"}/>
<Image Name="image1" Source="{Binding MyImage1.source"}/>
<Image Name="image2" Source="{Binding MyImage2.source"}/>
...
</Grid>
I want instaead of that XAML
file, to have the source as something like MyImages[0].source
so I could also loop over it and set it in runtime and not have to write MyImage0.source="mysource"
Upvotes: 1
Views: 1222
Reputation: 729
I would suggest the following:
Having a Collection of Type MyImage like:
public ObservableCollection<MyImage> MyImageCollection { get; set; }
and then go with a DataTemplate and ItemsControl in your View:
<Grid>
<ItemsControl ItemsSource="{Binding MyImageCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding source}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Upvotes: 5