2017-03-01 105 views
1

我剛開始一個新項目,並利用這個場合習慣了MVVM。綁定從ObservableCollection到Datagrid的列表中的類的屬性

但我很躊躇,特殊情況下,我嘗試將自定義類的列表從ObservableCollection綁定到Datagrid。 我使用以下星座,我可以完成的是顯示集合的Datagrid列。 https://snag.gy/2MDEuS.jpg

如果我試圖進一步推進到對象與它不是與下面的錯誤指示工作{綁定路徑= Supplier.Supplier},編譯器是無法從列表中讀出的屬性,如我解釋錯誤:

System.Windows.Data Error: 40 : 
    BindingExpression path error: 
    'Supplier' property not found on 'object' ''List`1' (HashCode=55391087)'. 
    BindingExpression:Path=Supplier.Supplier; 
    DataItem='O_SupplierReport' (HashCode=61342683); 
    target element is 'TextBlock' (Name=''); 
    target property is 'Text' (type 'String') 

還有其他的文本框,我可以很容易地結合= {綁定路徑= MySelectedItem.SupplierName}填充例如。 你能給我一個建議嗎?

//ViewModel 
public class V_SupplierReport: INotifyPropertyChanged 
    { 
     public ObservableCollection<O_SupplierReport> _SupplierReports; 
     public O_SupplierReport MySelectedItem { get; set; } 
     private List<S_Supplier> _Supplier { get; set; } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected internal void OnPropertyChanged(string propertyname) 
     { 
      if (!(PropertyChanged == null)) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
     } 
     public ObservableCollection<O_SupplierReport> SupplierReports 
     { 
      get { return _SupplierReports; } 
      set { _SupplierReports = value; } 
     } 
     public V_SupplierReport() 
     { 
      this._SupplierReports = new ObservableCollection<O_SupplierReport>(); 
     } 
     public int Lieferanten 
     { 
      get { return _Supplier; } 
      set 
      { 
       if (_Supplier == value) return; 
       _Supplier = value; 
       OnPropertyChanged("Supplier"); 
      } 
     } 
    } 

//Model 
public class O_SupplierReport : INotifyPropertyChanged 
    { 
     public List<S_Supplier> Supplier { get; set; } 

     public O_SupplierReport(List<S_Supplier> sup) 
     { 
      this.Supplier = sup; 
     } 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected internal void OnPropertyChanged(string propertyname) 
     { 
      if (!(PropertyChanged == null)) 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); 
     } 
    } 

//Classpublic class S_Supplier 
    { 
     public int Supplier { get; set; } 
     public S_Supplier(int sup) 
     { 
      Supplier = sup; 
     } 
    } 

    //View 
<Window x:Class="APP.SDX.SupplierReports.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="483.8" Width="640" DataContext="{Binding SupplierReports}"> 
    <Grid> 
     <DataGrid Name="G_lb_Selektion_Lieferanten" 
          Margin="0,26,0,27" 
          ItemsSource="{Binding Path=SupplierReports}" 
          SelectedItem="{Binding Path=MySelectedItem}" 
          AutoGenerateColumns="False"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Lieferant" Binding="{Binding Supplier}" /> 
       <DataGridTextColumn Header="Lieferant" Binding="{Binding Path=Supplier}" /> 
       <DataGridTextColumn Header="Lieferant" Binding="{Binding Path=Supplier.Supplier}" /> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 
+0

附加答案我找到了一個更合適的解決方案,無需Dat​​aGridTem plateColumn: ItemsSource =「{Binding Path = MySelectedItem.Supplier}」DisplayMemberPath =「Supplier」in the view did the trick too – kurdy

回答

0

DataGridTextColumn只能顯示類型TDataGridIEnumerable<T>的ItemsSource標量屬性的值。

如果你想顯示「Lieferant」欄目內的所有S_Supplier對象,你可以使用一個DataGridTemplateColumn與嵌套DataGrid

<DataGrid Name="G_lb_Selektion_Lieferanten" 
          Margin="0,26,0,27" 
          ItemsSource="{Binding Path=SupplierReports}" 
          SelectedItem="{Binding Path=MySelectedItem}" 
          AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="Lieferant" > 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <DataGrid ItemsSource="{Binding Supplier}" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

如果只有一個S_SupplierList<S_Supplier>對象和要顯示這個屬性可以使用索引器綁定到列表中的第一項:

<DataGridTextColumn Header="Lieferant" Binding="{Binding Supplier[0].Supplier }" /> 
+0

先生,你太棒了! – kurdy

相關問題