我刚刚开始了一个新项目,并利用这个机会来习惯 MVVM。
但是我在一个特殊情况下犹豫不决,我尝试将自定义类的列表从我的 ObservableCollection 绑定(bind)到 Datagrid。
我使用以下星座,我所能完成的只是显示集合的 Datagrid Column。
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>
最佳答案
一个 DataGridTextColumn
只能显示 T
类型的标量属性的值在 IEnumerable<T>
元素来源DataGrid
.
如果要显示全部S_Supplier
“Lieferant”列中的对象,您可以使用 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>
中的对象并且您想显示这个属性,您可以使用索引器绑定(bind)到列表中的第一项:<DataGridTextColumn Header="Lieferant" Binding="{Binding Supplier[0].Supplier }" />
关于c# - 将列表中类的属性从 ObservableCollection 绑定(bind)到数据网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42534652/