如何通过ViewModel将XamDataGrid绑定到Model Properties

时间:2013-04-19 14:42:29

标签: wpf xaml binding mvvm-light xamdatagrid

在我的C#/ WPF应用程序中,我正在尝试将Infragistics WPF XamDataGrid绑定到ObservableCollection。 MyViewModel公开了Model属性。当我将要显示其值的属性添加到MyViewModel时,一切正常。下面显示了一个示例,其中我绑定了UnitOfMeasure ObservableCollection中包含的MyViewModel实例公开的UnitID属性。

如果我将“Name”更改为“Model.Name”,则在运行时,数据网格中会显示正确的行数,但所有字段均为空白。不会引发运行时错误。

我的解决方法是将每个属性添加到MyViewModel,并在每个getter和setter中引用Model中的属性。制作冗余代码,并不同意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;
        }
    }

1 个答案:

答案 0 :(得分: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 =&gt;  是否要从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; }
        }
    }