C# WPF绑定到数组/集合
我有大约30个控件,每个都有相同的模型。我不想绑定30个属性和30个私有变量,而是想绑定到一个数组,这样我就可以在属性上循环 例如 假设我有以下内容,这只是一个例子C# WPF绑定到数组/集合,c#,wpf,xaml,C#,Wpf,Xaml,我有大约30个控件,每个都有相同的模型。我不想绑定30个属性和30个私有变量,而是想绑定到一个数组,这样我就可以在属性上循环 例如 假设我有以下内容,这只是一个例子 public class MyImage { public String source { get; set;} public String tooltip { get; set;} } xaml 我希望该XAML文件的instaead具有类似MyImages[0]的源代码 因此,我也可以在运行时循环并设置它,而不
public class MyImage
{
public String source { get; set;}
public String tooltip { get; set;}
}
xaml
我希望该XAML文件的instaead具有类似MyImages[0]的源代码
因此,我也可以在运行时循环并设置它,而不必编写MyImage0。source=mysource我建议如下: 具有MyImage类型的集合,如:
public ObservableCollection<MyImage> MyImageCollection { get; set; }
然后在视图中使用DataTemplate和ItemsControl:
<Grid>
<ItemsControl ItemsSource="{Binding MyImageCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding source}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
我建议如下: 具有MyImage类型的集合,如:
public ObservableCollection<MyImage> MyImageCollection { get; set; }
然后在视图中使用DataTemplate和ItemsControl:
<Grid>
<ItemsControl ItemsSource="{Binding MyImageCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image Source="{Binding source}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
考虑使用iTimeStand控件,用ItMsStand集合来收集MyIsIObjsObjts,但是我将如何绑定到它?MyIGeMe0,MyIGIVE1,MyIVIIG2不能在单个元素集合中吗?如果它们是同一类,那么它就不应该是问题。考虑使用ItMsStand控件和ItMsStand集合来收集MyI图对象,但是我将如何绑定到它呢?MyIGeMe0、MyIGIVE1、MyIVIIG2不能在单个元素集合中吗?如果他们是同一个班,那应该没问题。谢谢,我会试试的。我会试试的