2017-03-01 49 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

相关问题