根据对象类型使用不同的 itemcontainerstyles [英] using different itemcontainerstyles based on objecttype

查看:35
本文介绍了根据对象类型使用不同的 itemcontainerstyles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有集合的列表框

I have a Listbox with an collection

ObservableCollection<BaseObject> _baseObjects;
public ObservableCollection<BattlegroundBaseObject> BaseObject
{
    get { return _baseObjects?? (_baseObjects= new ObservableCollection<BaseObject>()); }
} 

该集合有两个来自 BaseObject 的不同子项.一个是路径,另一个是图像..更多即将推出

the collection has two different children from BaseObject. one is a path the other an image.. more are coming

我现在需要基于孩子的两个不同的 ItemContainerStyles

i need now two different ItemContainerStyles based on the childrens

<ListBox.ItemContainerStyle>

     <Style BasedOn="ListBoxItem" TargetType="ListBoxItem"  x:Name="ListBoxPathLineStyle">
        <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="ListBoxItem">
                     <Path Stroke="{Binding ObjectColor}" Data="{Binding PathGeometryData}" />                        
               </ControlTemplate>
             </Setter.Value>
      </Setter>

       <!-- Alternative Template for other type -->
      <Setter Property="Template">
           <Setter.Value>
               <ControlTemplate TargetType="ListBoxItem">
                   <Image Source="howTheHellCares.png"/>                                           
               </ControlTemplate>
           </Setter.Value>
      </Setter>               
   </Style> 
</ListBox.ItemContainerStyle>

目前总是采用较低的二传手,但我需要区分它..某人知道如何吗?

at the moment the lower setter is always taken, but i need to differ it.. sb knows how?

推荐答案

看来你对 ItemContainerStyle 有错误的想法...Style 用于容器,而不是内容.我的意思是它 StyleListBoxItem 而不是项目中包含的数据......字面上,项目的容器.

It appears that you have the wrong idea about the ItemContainerStyle... the clue is in the name... it is the Style for the container, not for the content. By that I mean that it Styles the ListBoxItem and not the data contained within the item... literally, the container of the item.

您真正要寻找的(似乎)是您可以为 ItemTemplate 属性指定的 DataTemplate.DataTemplate 具有专门的 DataType 属性,以便您可以利用相关类的属性.您可以使用DataTemplateSelector 在每个项目的基础上在 DataTemplate 之间切换,但还有另一种方式.

What (it seems) you are really looking for is the DataTemplate that you can specify for the ItemTemplate property. The DataTemplate has a DataType property specifically so that you can take advantage of the properties of the relevant class. You could use a DataTemplateSelector to switch between DataTemplates on a per item basis, but there is another way.

您可以做的是为您拥有的每种数据类型指定一个 DataTemplate,但不要给它们一个 x:Key.这样,WPF 会自动将它们应用到它发现没有明确设置的任何相关数据类型对象.

What you can do is to specify a DataTemplate for each data type you have, but don't give them an x:Key value. This way, WPF will automatically apply them to any relevant data type object that it finds that doesn't have one explicitly set.

<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:SomeDataType}">
    ...
</DataTemplate>
<DataTemplate DataType="{x:Type YourXmlNamespacePrefix:SomeOtherDataType}">
    ...
</DataTemplate>

您可以将 ItemContainerStyle 留空,也可以定义每个项目的容器外观,而不管类型如何.

You can either leave the ItemContainerStyle empty, or define what the container will look like for every item, regardless of the type.

这篇关于根据对象类型使用不同的 itemcontainerstyles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆