2

I want to show the status of my contract, both of which are declared as follows:

public enum RentStatus
{
    [Description("Preparation description")]
    Preparation,
    [Description("Active description")]
    Active,
    [Description("Rented to people")]
    Rented
}

public class RentContract
{
    public int RentContractId { get; set; }
    public virtual Premise Premise { get; set; }
    public double Price { get; set; }
    public RentStatus Status { get; set; }
}

My current XAML that is wrong

<ComboBox x:Name="RentStatusComboBox"
                ItemsSource="{Binding RentContract}"
                Grid.Row="2"
                Grid.Column="1"
                HorizontalAlignment="Stretch"
                SelectedItem="{Binding RentContract.Status}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Description}" Padding="0" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I've seen several solutions that use Converters and methods, but I don't think those are able to allow for databinding so that my entity is updated when I change the status in my UI.

Edit: Following this excellent blogpost solved my issue.

Benjamin Diele
  • 1,177
  • 1
  • 10
  • 26

1 Answers1

3

You can create a extenstion method for the Enum and using reflection you can set the description. refer the below code.

<ComboBox Width="200" Height="25" ItemsSource="{Binding ComboSource}"
              DisplayMemberPath="Value"
              SelectedValuePath="Key"/>
 public class MainViewModel
{
    public List<KeyValuePair<RentStatus, string>> ComboSource { get; set; }

    public MainViewModel()
    {
        ComboSource = new List<KeyValuePair<RentStatus, string>>();
        RentStatus re=RentStatus.Active;
        ComboSource = re.GetValuesForComboBox<RentStatus>();
    }
}

public enum RentStatus
{
    [Description("Preparation description")]
    Preparation,
    [Description("Active description")]
    Active,
    [Description("Rented to people")]
    Rented
}

public static class ExtensionMethods
{       
    public static List<KeyValuePair<T, string>> GetValuesForComboBox<T>(this Enum theEnum)
    {
        List<KeyValuePair<T, string>> _comboBoxItemSource = null;
        if (_comboBoxItemSource == null)
        {
            _comboBoxItemSource = new List<KeyValuePair<T, string>>();
            foreach (T level in Enum.GetValues(typeof(T)))
            {
                string Description = string.Empty;                    
                FieldInfo fieldInfo = level.GetType().GetField(level.ToString());
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);                    
                if (attributes != null && attributes.Length > 0)
                {
                    Description = attributes.FirstOrDefault().Description;
                }
                KeyValuePair<T, string> TypeKeyValue = new KeyValuePair<T, string>(level, Description);
                _comboBoxItemSource.Add(TypeKeyValue);
            }
        }
        return _comboBoxItemSource;
    }
}
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44
  • This looks nice, thank you. Is there a way to have these values databound to my `RentContract`, over which i'm loopîng in a Grid? I need to be able to change the `RentStatus` for a selected `RentContract`. – Benjamin Diele Dec 19 '14 at 20:13