Search code examples
c#wpfxamldatatemplate

Different Content (System.Windows.DataTemplates) displayed by ContentControl rather than required Icon


I am working on a combo box with icons.This combo box consist of items which have a name and an icon with the respective type it belongs to.

While my attempt has been successful to a certain extent I am getting a string which says "System.Windows.DataTemplate" to where it's supposed to show the respective icon.

I feel like there is something wrong in the way that I am calling the content controller.


Solution

  • you should use DataTemplate resources to set ContentTemplate property, not Content

    <ContentControl Content="{Binding}">
        <ContentControl.Style>
            <Style TargetType="{x:Type ContentControl}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type dc:ComboBoxItem}}, Path=DataContext, Converter={StaticResource FormBuilderClient_TypeOfConverter}}" Value="{x:Type models:FileSettingsModel}" >
                        <Setter Property="ContentTemplate" Value="{StaticResource FileIcon}" />
                    </DataTrigger>
    
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type dc:ComboBoxItem}}, Path=DataContext, Converter={StaticResource FormBuilderClient_TypeOfConverter}}" Value="{x:Type models:ServerSettingsModel}" >
                        <Setter Property="ContentTemplate" Value="{StaticResource ServerIcon}" />
                    </DataTrigger>
    
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type dc:ComboBoxItem}}, Path=DataContext, Converter={StaticResource FormBuilderClient_TypeOfConverter}}" Value="{x:Type models:HomeSettingsModel}" >
                        <Setter Property="ContentTemplate" Value="{StaticResource HomeIcon}" />
                    </DataTrigger>
    
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    
    </ContentControl>
    

    however it should be simpler to use DataTemplates for specific type, rather than x:Key. also define them in ComboBox resources.

    <ComboBox.Resources >
    
        <DataTemplate DataType="{x:Type models:FileSettingsModel}">
            <Path Stretch="Uniform" Stroke="Black" Fill="Blue" Height="15" Width="25" Data="..."/>
        </DataTemplate>
    
        <DataTemplate DataType="{x:Type models:ServerSettingsModel}" >
            <Path Stretch="Uniform" Stroke="Black"  Fill="OrangeRed" Height="15" Width="25" Data="..."/>
        </DataTemplate>
    
        <DataTemplate DataType="{x:Type models:HomeSettingsModel}">
            <Canvas>
                <Path  Canvas.Top="0" Canvas.Left="-2" Height="15" Width="25"
       Stretch="Uniform" Stroke="Black" Fill="Green" Data="..."/>
    
                <Path Canvas.Top="7" Canvas.Left="9" Height="16" Width="23" 
      Stretch="Uniform" Stroke="Black" Fill="Yellow" Data="..."/>
            </Canvas>
        </DataTemplate>
    
    </ComboBox.Resources>
    

    this way ContentControl should pick correct template without any triggers:

    <ContentControl Content="{Binding}"/>