Home > database >  How can I emit the string representation of a (enum) value inside a DataTemplate for its type?
How can I emit the string representation of a (enum) value inside a DataTemplate for its type?

Time:03-08

I have created a DataTemplate that represents an enum as an icon (inspired by this answer):

<DataTemplate DataType="{x:Type enums:EntryType}">
    <Control x:Name="EntryTypeControl"
             ToolTip="{Binding}" />
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}" Value="{x:Static enums:EntryType.Debug}">
            <Setter TargetName="ItemTypeControl" Property="Template" Value="{StaticResource DebugGlyph}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="{x:Static enums:EntryType.Info}">
            <Setter TargetName="ItemTypeControl" Property="Template" Value="{StaticResource InfoGlyph}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="{x:Static enums:EntryType.Warn}">
            <Setter TargetName="ItemTypeControl" Property="Template" Value="{StaticResource WarningGlyph}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="{x:Static enums:EntryType.Error}">
            <Setter TargetName="ItemTypeControl" Property="Template" Value="{StaticResource ErrorGlyph}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding}" Value="{x:Static enums:EntryType.Fatal}">
            <Setter TargetName="ItemTypeControl" Property="Template" Value="{StaticResource FatalGlyph}" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

This is working as intended except for the tooltip: I really wanted to display the original string representation of the enum (i.e. what I would get without the DataTemplate). However, what is really happening is that the tooltip just shows the icon again. How do I convince the template of not referring back to itself?

Obviously, this would work if I assigned a dedicated x:Key to the DataTemplate and explicitly referenced that but is this also possible without doing that?

CodePudding user response:

You could add a TextBlock to the ToolTip:

<Control x:Name="EntryTypeControl">
    <Control.ToolTip>
        <ToolTip>
            <TextBlock Text="{Binding}" />
        </ToolTip>
    </Control.ToolTip>
</Control>
  • Related