2015-05-01 90 views
0

我有幾個WPF窗口,數據綁定到視圖模型類,公開屬性。更改與MVVM對象綁定的顯示屬性

<Window x:Class="OIAFMS.Presentation.Win.Views.AllLedgerEntries" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:vm="clr-namespace:OIAFMS.Presentation.Win.ViewModels"  
    Height="350" Width="525"> 
<Window.DataContext> 
    <vm:AllLedgerEntriesViewModel /> 
</Window.DataContext> 
<Grid> 
    <DataGrid Grid.Row="0" ItemsSource="{Binding Path=ViewModels}" /> 
</Grid> 

這關係回的一類,其暴露的ViewModels屬性:

public List<LedgerEntryViewModel> ViewModels { 
     get { 
      List<LedgerEntryViewModel> collection = new List<LedgerEntryViewModel>(); 
      foreach (var item in _Service.GetAll()) { 
       collection.Add(new LedgerEntryViewModel(item, _Service)); 
      } 

      return collection; 
     } 
    } 

內的個別型號,我有以下幾點:

public decimal ContributionAmt { 
     get { return _Model.ContributionAmt; } 
     set { 
      _Model.ContributionAmt = value; 
      OnPropertyChanged("ContributionAmt"); 
     } 
    } 

我'試圖做的是看我是否可以添加DisplayName屬性或其他內容,以便我可以更改w這在屏幕上顯示(從「ContributionAmt」到「貢獻金額」)。

[DisplayName("Contribuion Amount")] 
    public decimal ContributionAmt { 
     get { return _Model.ContributionAmt; } 
     set { 
      _Model.ContributionAmt = value; 
      OnPropertyChanged("ContributionAmt"); 
     } 
    } 

因爲它是現在,這是它是如何顯示。

DataGrid displayed

有什麼辦法,我可以去嗎?

回答

1

Got it!

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { 
     string displayName = ((PropertyDescriptor)e.PropertyDescriptor).DisplayName as string; 

     if (displayName != null) { 
      e.Column.Header = displayName; 
     } 
    }