[英]How to determine the specifix type of a item in a ObservableCollection<object> and bind to a property of the item in wpf?
I've got a ObservableCollection<IContainers> Containers
which defines the property ObservableCollection<object> Content
. 我有一个
ObservableCollection<IContainers> Containers
,该ObservableCollection<IContainers> Containers
定义了ObservableCollection<object> Content
属性。
public interface IContainers
{
public double Height {get; set;}
public double Width {get; set;}
public ObservableCollection<object> Content {get; set;}
}
public class SetupStep
{
public ObservableCollection<IContainer> Containers {get; set;}
}
The types of items in the Content
Property can vary. Content
属性中的项目类型可以有所不同。 These types define their own properties to which I want to bind. 这些类型定义了我要绑定的自己的属性。
This is my .xaml code: 这是我的.xaml代码:
<c:ScatterView
ItemsSource="{Binding Containers}">
<c:ScatterView.ItemTemplate>
<DataTemplate>
<ListBox
ItemsSource="{Binding Content}">
<!--A way to determine my types in Contents?!?!-->
<ListBox.Resources>
<DataTemplate x:Key="{x:Type myObjects:Picture}">
<Image Source="{Binding Picture.FullFileName}"/>
</DataTemplate>
<DataTemplate x:Key="{x:Type myObjects:Parameter}">
<myControl:ParameterControl Id="{Binding Parameter.Id}"/>
</DataTemplate>
</ListBox.Resources>
<!--A way to determine my types in Contents?!?!-->
</ListBox>
</DataTemplate>
</c:ScatterView.ItemTemplate>
</c:ScatterView>
I found no working solution to bind to the properties of myObjects like Picture
or Parameter
. 我找不到绑定到myObjects的属性(例如
Picture
或Parameter
有效解决方案。
I hope for some ideas :) Thanks, Alex 我希望有一些想法:)谢谢,亚历克斯
Data Templates and Binding source properties are resolved by reflection. 数据模板和绑定源属性通过反射解决。 So if the
Content
collection contains a Picture
, a DataTemplate for Picture
can be applied automatically. 因此,如果
Content
集合包含一个Picture
,一个DataTemplate中Picture
可以自动应用。
The only thing you need to do is to set the DataType
property of the DataTemplate: 您唯一需要做的就是设置DataTemplate的
DataType
属性:
<ListBox.Resources>
<DataTemplate DataType="{x:Type myObjects:Picture}">
<Image Source="{Binding Picture.FullFileName}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type myObjects:Parameter}">
<myControl:ParameterControl Id="{Binding Parameter.Id}"/>
</DataTemplate>
</ListBox.Resources>
Everything else should work out of the box. 其他所有东西都应该开箱即用。
While you could also use a DataTemplateSelector
, the above approach is far simpler. 虽然您也可以使用
DataTemplateSelector
,但是上述方法要简单得多。 Using a DataTemplateSelector isn't necessary unless you want to have different DataTemplates for different items of the same type, eg depending on the value of some property of the item class. 除非您要为同一类型的不同项目使用不同的DataTemplates,否则不需要使用DataTemplateSelector,例如,取决于item类的某些属性的值。
You might want to take a look at the DataTemplateSelector class . 您可能需要看一下DataTemplateSelector类 。 This allows you to switch
DataTemplate
depending on different criteria - in your case, this criteria could be the list item type: 这使您可以根据不同的条件切换
DataTemplate
在您的情况下,此条件可以是列表项类型:
public class CustomDataTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is MainWindow.Picture)
return PictureTemplate;
if (item is MainWindow.Parameter)
return ParameterTemplate;
// return some default template as fall-back
}
public DataTemplate PictureTemplate { get; set; }
public DataTemplate ParameterTemplate { get; set; }
// ...add other template references here...
}
You can now define all templates as XAML resources, and simply reference the TemplateSelector within the ListBox
: 现在,您可以将所有模板定义为XAML资源,并且只需引用
ListBox
的TemplateSelector即可:
<Window.Resources>
<DataTemplate x:Key="PictureTemplate">
<Image Source="{Binding FullFileName}"/>
</DataTemplate>
<DataTemplate x:Key="ParameterTemplate">
<myControl:ParameterControl Id="{Binding Id}"/>
</DataTemplate>
...add other templates here...
<local:CustomDataTemplateSelector x:Key="CustomDataTemplateSelector"
PictureTemplate="{StaticResource PictureTemplate}"
ParameterTemplate="{StaticResource ParameterTemplate}"/>
</Window.Resources>
<ListBox
ItemsSource="{Binding Content}"
ItemTemplateSelector="{StaticResource CustomDataTemplateSelector}">
</ListBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.