C# 将列表中类的属性从ObservableCollection绑定到Datagrid
我刚开始一个新项目,并利用这个机会来适应MVVM 但我遇到了一个特殊情况,我试图将一个自定义类列表从ObservableCollection绑定到Datagrid。 我使用以下星座图,我所能完成的就是显示集合的Datagrid列。 如果我尝试使用{Binding Path=Supplier.Supplier}进一步深入对象,则它不会处理以下错误,这表明编译器无法从列表中读取属性,因为我解释了错误:C# 将列表中类的属性从ObservableCollection绑定到Datagrid,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,我刚开始一个新项目,并利用这个机会来适应MVVM 但我遇到了一个特殊情况,我试图将一个自定义类列表从ObservableCollection绑定到Datagrid。 我使用以下星座图,我所能完成的就是显示集合的Datagrid列。 如果我尝试使用{Binding Path=Supplier.Supplier}进一步深入对象,则它不会处理以下错误,这表明编译器无法从列表中读取属性,因为我解释了错误: System.Windows.Data Error: 40 : BindingExp
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>
//视图模型
公共类V_供应商报告:InotifyProperty已更改
{
公共可观察收集(供应商报告);;
公共O_供应商报告MySelectedItem{get;set;}
私有列表{get;set;}
公共事件属性更改事件处理程序属性更改;
受保护的内部无效OnPropertyChanged(字符串propertyname)
{
如果(!(PropertyChanged==null))
PropertyChanged(这是新的PropertyChangedEventArgs(propertyname));
}
公共可观察收集供应商报告
{
获取{return\u SupplierReports;}
设置{u SupplierReports=value;}
}
公共V_供应商报告()
{
此._SupplierReports=新的可观察收集();
}
利弗兰滕公共酒店
{
获取{return\u Supplier;}
设置
{
如果(_Supplier==value)返回;
_供应商=价值;
不动产变更(“供应商”);
}
}
}
//模型
公共类O_供应商报告:InotifyProperty已更改
{
公共列表供应商{get;set;}
公共O_供应商报告(列表支持)
{
这个。供应商=sup;
}
公共事件属性更改事件处理程序属性更改;
受保护的内部无效OnPropertyChanged(字符串propertyname)
{
如果(!(PropertyChanged==null))
PropertyChanged(这是新的PropertyChangedEventArgs(propertyname));
}
}
//Classpublic ClassS_供应商
{
公共int提供程序{get;set;}
公共服务供应商(内部支持)
{
供应商=供应商;
}
}
//看法
ADataGridTextColumn
只能在DataGrid
的IEnumerable
ItemsSource中显示类型为T
的标量属性的值
如果要在“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>
DataGridTextColumn
只能显示DataGrid
的IEnumerable
项源中类型为T
的标量属性的值
如果要在“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>
先生,你真棒!先生,你真棒!除了答案之外,我还发现了一个更合适的解决方案,而不需要DataGridTemplateColumn:ItemsSource=“{Binding Path=MySelectedItem.Supplier}”DisplayMemberPath=“Supplier”在视图中,这个窍门是不是对答案进行了补充?我发现了一个更合适的解决方案,而不需要DataGridTemplateColumn:ItemsSource=”视图中的{Binding Path=MySelectedItem.Supplier}“displaymberpath=“Supplier”也起了作用