How to show different text based on an enum value?

This question was asked on the MSDN forums, so I thought I'd replciate it here for everybody's benefit.

When you want to bind to a value that's an enumeration, sometimes you want to show specific text. While writting a converter is one way of solving this problem, it requires writting code. And no code is of better quality than the one you don't write.

So here's the sample I posted.

MainWindow.xaml:

    1 <Window x:Class="MsdnForums.TextBlockBoundToEnum.MainWindow"

    2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4     xmlns:local="clr-namespace:MsdnForums.TextBlockBoundToEnum"

    5     Title="TextBlockBoundToEnum" Height="300" Width="300"

    6     >

    7     <!-- Forums: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1107413&SiteID=1 -->

    8     <DockPanel>

    9         <StackPanel DockPanel.Dock="Right">

   10             <TextBlock>Current CurState value:</TextBlock>

   11             <Button Click="HandleGoodButtonClick">Good</Button>

   12             <Button Click="HandleBadButtonClick">Bad</Button>

   13             <Button Click="HandleUglyButtonClick">Ugly</Button>

   14         </StackPanel>

   15         <TextBlock>

   16             <TextBlock.Style>

   17                 <Style TargetType="{x:Type TextBlock}">

   18                     <Style.Triggers>

   19                         <DataTrigger Binding="{Binding Path=CurState}" Value="Good">

   20                             <Setter Property="TextBlock.Text" Value="That was a good one!" />

   21                         </DataTrigger>

   22                         <DataTrigger Binding="{Binding Path=CurState}" Value="Bad">

   23                             <Setter Property="TextBlock.Text" Value="That's no good!" />

   24                         </DataTrigger>

   25                         <DataTrigger Binding="{Binding Path=CurState}" Value="Ugly">

   26                             <Setter Property="TextBlock.Text" Value="Ooooooooooh look at hiiiim!" />

   27                         </DataTrigger>

   28                     </Style.Triggers>

   29                 </Style>

   30             </TextBlock.Style>

   31         </TextBlock>

   32     </DockPanel>

   33 </Window>

MainWindow.cs

    1 using System;

    2 using System.Windows;

    3 

    4 namespace MsdnForums.TextBlockBoundToEnum

    5 {

    6     public partial class MainWindow : System.Windows.Window

    7     {

    8         private MyDataContext myDataContext;

    9         public MainWindow()

   10         {

   11             InitializeComponent();

   12             this.DataContext = myDataContext = new MyDataContext();

   13         }

   14         public void HandleGoodButtonClick(object source, RoutedEventArgs e)

   15         {

   16             myDataContext.CurState = APP_STATE.Good;

   17         }

   18         public void HandleBadButtonClick(object source, RoutedEventArgs e)

   19         {

   20             myDataContext.CurState = APP_STATE.Bad;

   21         }

   22         public void HandleUglyButtonClick(object source, RoutedEventArgs e)

   23         {

   24             myDataContext.CurState = APP_STATE.Ugly;

   25         }

   26     }

   27     public class MyDataContext : DependencyObject

   28     {

   29         public APP_STATE CurState

   30         {

   31             get { return (APP_STATE)GetValue(CurStateProperty); }

   32             set { SetValue(CurStateProperty, value); }

   33         }

   34         public static readonly DependencyProperty CurStateProperty =

   35             DependencyProperty.Register("CurState", typeof(APP_STATE), typeof(MyDataContext), new UIPropertyMetadata(APP_STATE.Good));

   36     }

   37     public enum APP_STATE

   38     {

   39         Good,

   40         Bad,

   41         Ugly

   42     }

   43 }

Ads

Comment