在我的 C#/WPF 应用程序中,我正在尝试将 Infragistics WPF XamDataGrid 绑定到 ObservableCollection。MyViewModel 公开 Model 属性。当我将要显示其值的属性添加到 MyViewModel 时,一切正常。下面显示了一个示例,其中我绑定到由 UnitsOfMeasure ObservableCollection 中包含的 MyViewModel 实例公开的 UnitID 属性。
如果我将“名称”更改为“模型名称”,则在运行时数据网格中会出现正确的行数,但所有字段都是空白的。不会引发运行时错误。
我的解决方法是将每个属性添加到 MyViewModel 并在每个 getter 和 setter 中引用模型中的属性。造成冗余代码,不同意 Laurent 的演示。
这个 xaml 有效:
<igEditors:XamDataGrid Theme="Royale" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" Margin="2" Name="xamDataGridUnitsOfMeasure" DataSource="{Binding Path=UnitsOfMeasure}" VerticalAlignment="Stretch">
<igEditors:XamDataGrid.FieldLayoutSettings>
<igEditors:FieldLayoutSettings AutoGenerateFields="False"/>
</igEditors:XamDataGrid.FieldLayoutSettings>
<igEditors:XamDataGrid.FieldLayouts>
<igEditors:FieldLayout IsDefault="True">
<igEditors:FieldLayout.Fields>
<igEditors:Field Name="UnitID" Label="UOM ID">
<igEditors:Field.Settings>
<igEditors:FieldSettings AllowEdit="False"/>
</igEditors:Field.Settings>
</igEditors:Field>
.
.
.
将 UnitID 更改为 Model.UnitID 不起作用:
<igEditors:XamDataGrid Theme="Royale" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Stretch" Margin="2" Name="xamDataGridUnitsOfMeasure" DataSource="{Binding Path=UnitsOfMeasure}" VerticalAlignment="Stretch">
<igEditors:XamDataGrid.FieldLayoutSettings>
<igEditors:FieldLayoutSettings AutoGenerateFields="False"/>
</igEditors:XamDataGrid.FieldLayoutSettings>
<igEditors:XamDataGrid.FieldLayouts>
<igEditors:FieldLayout IsDefault="True">
<igEditors:FieldLayout.Fields>
<igEditors:Field Name="Model.UnitID" Label="UOM ID">
<igEditors:Field.Settings>
<igEditors:FieldSettings AllowEdit="False"/>
</igEditors:Field.Settings>
</igEditors:Field>
.
.
.
这是功能性 ViewModel 的一部分:
public class UnitOfMeasureViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the UnitOfMeasureViewModel class.
/// </summary>
public UnitOfMeasureViewModel(UnitOfMeasure model)
{
Model = model;
//Model.PropertyChanged += (s, e) =>
// {
// RaisePropertyChanged(e.PropertyName);
// };
}
public UnitOfMeasure Model
{
get;
private set;
}
public string UnitID
{
get
{
return Model.UnitID;
}
set
{
Model.UnitID = value;
}
}