2017-03-01 9 views
1

私はちょうど新しいプロジェクトを開始し、この機会を使ってMVVMに慣れました。ObservableCollectionからDatagridへのリストのクラスのプロパティのバインド

しかし私はObservableCollectionのカスタムクラスのリストをDatagridにバインドしようとする特別なケースでは嫌です。 私は以下の星座を使用し、私が達成できるのは、コレクションを示すDatagrid列です。 https://snag.gy/2MDEuS.jpg

私はそれを示し、次のエラーで作業aintの{バインディングパス= 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') 

他のテキストボックスがあり、Iは容易例えばバインディング= {バインディングパス= 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

答えに追加して、DataGridTem plateColumn: ItemsSource = "{バインディングパス= MySelectedItem.Supplier}"ビューのDisplayMemberPath = "Supplier"もトリックを行いました – kurdy

答えて

0

DataGridTextColumnのみDataGridIEnumerable<T>のItemsSourceタイプTのスカラープロパティの値を表示することができます。

あなたは、ネストされたDataGridDataGridTemplateColumnを使用することができます「Lieferant」列内のすべてのS_Supplierのオブジェクトを表示したい場合は、次の

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

List<S_Supplier>で唯一S_Supplierオブジェクトがある場合、あなたが表示したいですこれのプロパティは、インデクサーを使用してリストの最初の項目にバインドすることができます。

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

サー、あなたは最高です! – kurdy

関連する問題