using different itemcontainerstyles based on objecttype

佐手、 提交于 2019-12-13 01:34:55

问题


I have a Listbox with an collection

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

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

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?


回答1:


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.

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.

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>

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




回答2:


<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>

      <Style.Triggers>
          <DataTrigger Binding={Binding Path=., Converter={StaticResource MyTypeToTemplaceConverter}} Value="True">
                <Setter Property="Template">
                    <Setter.Value>                                                                
                        <ControlTemplate TargetType="ListBoxItem">
                            <Image Source="howTheHellCares.png"/>                                           
                        </ControlTemplate>                        
                    </Setter.Value>
                </Setter>  
           </DataTTrigger>
      </Style.Triggers>   
    </Style> 
</ListBox.ItemContainerStyle>


来源:https://stackoverflow.com/questions/20099688/using-different-itemcontainerstyles-based-on-objecttype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!