2015-12-31 70 views
0

在我的DataGrid中,我有三列,行數是動態的。 DataGrid的值是雙數組。我怎樣才能結合每個陣列到他的專欄,而無需創建一個新的類(我propertychangedevent每個陣列)DataGrid - 綁定雙列到列

<DataGrid Name="dataGrid" HorizontalContentAlignment="Center" VerticalContentAlignment="Stretch" 
           AutoGenerateColumns="False" Style="{DynamicResource DataGridStyle1}" 
           CellEditEnding="dataGrid_Kennlinie_CellEditEnding" BeginningEdit="dataGrid_Kennlinie_BeginningEdit" 
           MaxWidth="500"> 
         <DataGrid.Columns> 
          <DataGridTextColumn Header="nue" Binding="{Binding nue}" Width="*"> 
           <DataGridTextColumn.Foreground> 
            <SolidColorBrush Color="Black"/> 
           </DataGridTextColumn.Foreground> 
          </DataGridTextColumn> 

          <DataGridTextColumn Header="mue" Binding="{Binding mue}" Width="*"/> 
          <DataGridTextColumn Header="tpc[Nm]" Binding="{Binding MPc}" Width="*" /> 
....end 

NUE,MUE和MPC的插圖中另一級陣列。當我只是做

dataGrid.ItemSource = class.nue; 

在這個類中創建,其中包括我所需要的變量的類,它們是:

private double[] _nue; 
    public double[] nue 
    { 
     get { return _nue; } 
     set 
     { 
      if (_nue == value) return; 
      _nue = value; 
      OnPropertyChanged("_nue"); 
     } 
    } 
    private double[] _mue; 
    public double[] mue 
    { 
     get { return _mue; } 
     set 
     { 
      if (_mue == value) return; 
      _mue = value; 
      OnPropertyChanged("_mue"); 
     } 
    } 
    private double[] _MPc; 
    public double[] MPc 
    { 
     get { return _MPc; } 
     set 
     { 
      if (_MPc == value) return; 
      _MPc = value; 
      OnPropertyChanged("_MPc"); 
     } 
    } 

這樣能讓我行,但沒有值正確的號碼。

任何想法? 感謝和新年快樂

+0

您應該將用作數據源的類的代碼。 – Aybe

+0

我不需要整個班級,只有三個變量。 –

回答

1

在你的例子中,你只是綁定到一個數組(nue)。 因此行數是正確的,但沒有顯示數據。因爲nue.nue, nue.mue等不存在。 (您應該會在輸出窗口中看到關於缺少綁定的錯誤。)

最簡單的解決方案是重構您的類,並綁定該類的List,Array或ObervableCollection。

如果您從某處獲得雙數組作爲輸入:沒有辦法在其他結構中映射此數據,DataGrid可以默認處理這些數據。

// simplified 
class Container { 
    public double Nue {get; set;} 
    public double Mue {get; set;} 
    public double MPc {get; set;} 
} 

ObservableCollection<Container> containers = ... 
dataGrid.ItemSource = containers; 

// This will now update the grid, just like other operations on the ObservableCollection 
containers.Add(new Container { 
    Nue = 13.1, 
    Mue = 2.23, 
    MPc = 0.01 
}); 

數據網格XAML可以保持原樣,綁定現在應該工作。

+0

但我沒有我的原始屬性changedchanged事件,我只是用我需要的值寫入新的變量 –

+0

容器可以實現INotifyPropertyChanged,你可以在它的屬性上引發OnPropertyChanged。 – jHilscher

+0

但後來有雙打。所以,當我提出一個事件,它只改變了雙倍的價值,而不是整個陣列 –