【问题标题】:WPF Binding to parent object in an XamDataGridWPF 绑定到 XamDataGrid 中的父对象
【发布时间】:2021-08-27 14:45:01
【问题描述】:

我继承了一个使用 WPF 的旧项目。我们利用基础设施。 下面是一个组成的类结构:

public class DogsVM
{
    public string ViewName { get; set; } = "My Dogs";
    public List<Dog> Dogs { get; set; }
}

public class Dog
{
    public string DogName { get; set; }
    public string Description { get; set; }
}

我正在使用XamDataGrid 来显示我的数据。

XamDataGrid 上的当前数据源是DataSource="{Binding CollectionView}"

当我输出字段时,我正在使用以下内容

<igDP:Field Name="DogName " Label="Dog Name" AllowEdit="False" />

我希望将标签更改为 DogsVM 并选择字段 ViewName

如果我这样做

<igDP:Field Name="DogName " Label="{Binding ViewName}" AllowEdit="False" />

DogName 输出为我正在查看Dog 对象,而不是DogsVM 对象。如何在标签绑定中到达父对象?

【问题讨论】:

  • 这是一个简化的例子。标签将是动态创建的,而不是硬编码的字符串。

标签: wpf xaml infragistics legacy-code xamdatagrid


【解决方案1】:

所以你有一些具有名为 CollectionView 的属性的对象。该对象是 XamDataGrid 的 DataContext。您应该向 that 相同的对象添加一个字符串属性,该对象具有您要用于该字段标签的值。然后使用FieldBinding 将该字段的属性绑定到该属性。如果该对象是 DogsVM 类,而您只是不在那里显示 CollectionView,那么它将类似于 {FieldBinding ViewName}

【讨论】:

    【解决方案2】:

    On the Infragistics site there is the following explanation for a similar problem:

    XamDataGrid 中的字段不是 WPF 中的可视元素,因此 不能直接绑定到数据上下文,因为它们不公开一个 继承自FrameworkElement

    为了绑定非可视元素的属性 XamDataGridFieldFieldSettingsFieldLayoutSettings,我 建议使用FieldBinding。你可以阅读FieldBindingXamDataGrid 这里: https://www.infragistics.com/help/wpf/xamdatagrid-binding-field-fieldlayout-to-mvvm.

    因此,建议在 Infragistics 网站上使用 FieldBinding 标记扩展,以便将属性绑定到 FieldFieldSettingsFieldLayoutSettings
    虽然提到的帖子包含使用 MVVM 模式的示例,但 FieldBinding 标记扩展可以在没有它的情况下使用。

    关于上面的问题考虑在构造函数中设置DataContext

    public YouWindowConstructor()
    {        
        InitializeComponent();
        DataContext = new DogsVM();
    }
    

    现在将DataSource 设置为XamDataGrid 并使用FieldBinding 标记扩展:

    
    <igDP:XamDataGrid DataSource="{Binding Path=Dogs}" AutoFit="True">
        <igDP:XamDataGrid.FieldLayoutSettings>            
            ...
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:FieldLayout.Fields>    
                        ...
                        <igDP:Field Name="DogName" Label="{igDP:FieldBinding ViewName}" AllowEdit="False" />   
                        ...              
                    </igDP:FieldLayout.Fields>
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
    </igDP:XamDataGrid>        
        
    

    查看类似内容:https://stackoverflow.com/a/64545966/6630084

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-03
      • 2011-04-24
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2013-02-02
      • 2012-11-05
      相关资源
      最近更新 更多