wpf 如何通过 ViewModel 将 XamDataGrid 绑定到模型属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16107252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:38:03  来源:igfitidea点击:

How to bind XamDataGrid to Model Properties through ViewModel

wpfxamlbindingmvvm-lightxamdatagrid

提问by Seth Abady

In my C#/WPF app, I and am trying to bind an Infragistics WPF XamDataGrid to an ObservableCollection. MyViewModel exposes the Model property. When I add the property whose value I want displayed to MyViewModel, everything works fine. Below shows an example where I'm binding to the UnitID property exposed by an instance of a MyViewModel contained in the UnitsOfMeasure ObservableCollection.

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

If I change "Name" to "Model.Name", at runtime the correct number of rows appears in the data grid, but all fields are blank. No runtime error is raised.

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

My workaround is to add each property to MyViewModel and reference the property in the Model in each getter and setter. Makes for redundant code, and doesn't agree with Laurent's demo's.

我的解决方法是将每个属性添加到 MyViewModel 并在每个 getter 和 setter 中引用模型中的属性。造成冗余代码,与 Laurent 的演示不一致。

This xaml works:

这个 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>
  .
  .
  .

Changing UnitID to Model.UnitID does not work:

将 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>
  .
  .
  .

Here's part of the ViewModel that is functional:

这是功能性的 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;
        }
    }

回答by RockWorld

The nested binding is supported by XamGrid control - http://help.infragistics.com/Help/NetAdvantage/WPF/2012.1/CLR4.0/html/xamGrid.html

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

Or

或者

Refer to this link of Infragistics help -http://www.infragistics.com/community/forums/t/67603.aspx

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

This will help you do the nested binding

这将帮助您进行嵌套绑定

Else => Do you want to extract interface out of Model object class and try to implement it at the class holding Model as property?

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; }
        }
    }