2017-10-10 34 views
0

我想將Enum int值綁定到datagridtextboxcolumn。 我使用下面如何將enum的整數值綁定到wpf中的datagridtext列

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" /> 

在這裏,我要綁定枚舉值即。 1,2,3等在xxx posotion

因爲我沒有太多專家在wpf,請幫助如何綁定這個。

謝謝。

回答

0

DataGridTextColumn顯示ToString()方法調用的結果。通過使用格式爲「D」的ToString,可以獲得enum的數值。與結合添加的StringFormat獲得相同的結果:

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

我試圖這樣

+0

但我在DataGridTextColumn中沒有任何值。 –

+0

@babucr,請提供您的視圖模型的更多細節。我的建議應該適用於任何'enum'屬性 - **確保Path使用視圖模型**中正確的屬性名稱。什麼是DataGrid的DataContext和ItemsSource? DataGridTextColumn顯示枚舉名/什麼都沒有StringFormat? (如果不是 - 它是不正確的綁定路徑) – ASh

相關問題