如何将enum的整数值绑定到wpf

时间:2017-10-10 16:00:03

标签: wpf enums datagrid

我想将Enum int值绑定到datagridtextboxcolumn。 我使用下面的代码

public enum Enm_Purchase_Ret : short
{
    Purchase = 1,
    Sale = 2,
    Return = 3
}


public class Vm_Purchase : INotifyPropertyChanged
{
    private Enumitem EnumItem = new Enumitem { Enm_Purchase_Rets = Enm_Purchase_Ret.Purchase };
    public Vm_Purchase()
    {

    }
    public class Enumitem
    {
        public Enm_Purchase_Ret Enm_Purchase_Rets { get; set; }

    }

    public Enumitem TestenumClass
    {
        get { return this.EnumItem; }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string PropertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new 
            PropertyChangedEventArgs(PropertyName));
        }
    }
}

在XAML中                                                          

 <DataGridComboBoxColumn Header="Value" ItemsSource="{Binding Source={StaticResource GetEnumValues}, UpdateSourceTrigger=PropertyChanged}" Width="100"
             SelectedItemBinding ="{Binding Enm_Purchase_Rets, Mode=TwoWay}" />


            <DataGridTextColumn Binding="{Binding xxx}" Header="Enum Id" Width="80" />

这里我想绑定Enum Value即。 xxx posotion中的1,2,3等

由于我不是wpf的专家,请帮助如何绑定它。

感谢。

1 个答案:

答案 0 :(得分:0)

DataGridTextColumn显示ToString()方法调用的结果。通过使用格式为“D”的ToString可以获得枚举的数值。要通过绑定获得相同的结果,请添加StringFormat:

Binding="{Binding Path=Enm_Purchase_Rets, StringFormat='\{0:D\}'}"