Search This Blog

Monday, August 22, 2016

Binding ComboBox to Enum and use Tag as SelectedValuePath

WPF > Controls > ItemsControl > ComboBox > Bind ComboBox to Enum

XAML


<Window x:Class="ComboBoxTag.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox
                  SelectedValue="{Binding Rate, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  SelectedValuePath="Tag" Margin="0,0,306,295">
            <!-- Tag is values of Rate enum -->
            <ComboBoxItem Tag="Bad">Bad</ComboBoxItem>
            <ComboBoxItem Tag="Low">Low</ComboBoxItem>
        </ComboBox>
    </Grid>

</Window>

C#

namespace ComboBoxTag
{
    public enum Rate
    {
        Bad = 0,
        Good = 1
    }
}