将列表中的类的属性从ObservableCollection绑定到Datagrid

时间:2017-03-01 14:42:56

标签: c# wpf xaml mvvm datagrid

我刚开始一个新项目,并利用这个机会习惯了MVVM。

但我在一个特殊情况下步履蹒跚,我尝试将一个自定义类的列表从我的ObservableCollection绑定到一个Datagrid。 我使用以下星座,我可以完成的是显示集合的Datagrid列。 https://snag.gy/2MDEuS.jpg

如果我尝试使用{Binding Path = 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')

还有其他文本框,我可以轻松填写​​Binding = {Binding Path = 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>

1 个答案:

答案 0 :(得分:0)

DataGridTextColumn只能在T的{​​{1}} ItemsSource中显示IEnumerable<T>类型的标量属性值。

如果要在“Lieferant”列中显示所有DataGrid个对象,可以使用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_Supplier对象,并且您想要显示此属性,则可以使用索引器绑定到列表中的第一个项目:

List<S_Supplier>