1

在我的 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;
        }
    }
4

1 回答 1

2

XamGrid 控件支持嵌套绑定 - http://help.infragistics.com/Help/NetAdvantage/WPF/2012.1/CLR4.0/html/xamGrid.html

或者

请参阅 Infragistics 帮助的此链接 - http://www.infragistics.com/community/forums/t/67603.aspx

这将帮助您进行嵌套绑定

Else => 你想从 Model 对象类中提取接口并尝试在持有 Model 作为属性的类中实现它吗?

 public interface IModel
    {
        public string UnitId { get; set; }
    }

    public class Model : IModel
    {
        private string unitId;
        public string UnitId
        {
            get { return unitId; }
            set { unitId = value; }
        }
    }

    public class ModelParent : IModel
    {
        private Model model;
        public string UnitId
        {
            get { return model.UnitId; }
            set { model.UnitId = value; }
        }
    }
于 2013-04-19T15:10:44.103 回答