简体   繁体   中英

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.

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}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM