Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top