简体   繁体   English

如何确定ObservableCollection中项目的指定类型 <object> 并绑定到WPF中的项目的属性?

[英]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的属性(例如PictureParameter有效解决方案。

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.

相关问题 .NET MAUI - 如何绑定 ObservableCollection 项的属性 - .NET MAUI - How to bind on a property of an ObservableCollection Item WPF从observablecollection中的选定项目绑定命令属性 - WPF Bind command property from selected item in observablecollection 如何将组合框绑定到ObservableCollection列表中的某个项目-WPF? - How to bind combo-box to a certain item in an ObservableCollection list - WPF? 订阅ObservableCollection项属性已更改 - WPF - Subscribe to ObservableCollection item property changed - WPF WPF-如何将所选项目从数据网格加载到ObservableCollection类型 - WPF - How to load selected item from a data grid to observablecollection type 如何从XML中的ObservableCollection绑定ModelView项目? - How to bind a ModelView item from a ObservableCollection in xml? 如何将 WPF ListBoxItem 的 IsEnabled 属性绑定到其源项的属性? - How to bind the IsEnabled Property of a WPF ListBoxItem to a Property of its Source Item? 如何确定是否选择了WPF Datagrid项并将其绑定到正确/错误结果 - How to determine if an WPF Datagrid item is selected, and bind to the true/false result 查找ObservableCollection项目的类型 - Find type of ObservableCollection Item 如何从WPF DataGrid中的更改更新ObservableCollection项的属性? - How do I update an ObservableCollection item's property from change in a WPF DataGrid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM