用户控件的子依赖对象上的绑定不起作用

Bindings on child dependency object of usercontrol not working(用户控件的子依赖对象上的绑定不起作用)
本文介绍了用户控件的子依赖对象上的绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使绑定​​在用户控件的子对象上工作.Xaml 看起来像这样:

I'm trying to get a binding to work on a child object of a user control. The Xaml looks like this:

<MyGrid>
    <MyColumn ExtendedColumnData="{Binding ColumnToolTipDescriptions}"/>
</MyGrid>

以下是类的定义方式:

[ContentProperty("Columns")]
public class MyGrid : UserControl
{
    private MyColumnCollection _columns;

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Category("Data")]
    public MyColumnCollection Columns
    {
        get
        {
            if (_columns == null)
                _columns = new MyColumnCollection();

            return _columns;
        }
    }
}

public class MyColumnCollection : ObservableCollection<MyGridColumn>
{
}

public class MyGridColumn : DependencyObject
{
    public object ExtendedColumnData
    {
        get { return (object)GetValue(ExtendedColumnDataProperty); }
        set { SetValue(ExtendedColumnDataProperty, value); }
    }

    public static readonly DependencyProperty ExtendedColumnDataProperty =
        DependencyProperty.Register("ExtendedColumnData", typeof(object), typeof(MyGridColumn), new UIPropertyMetadata(null));
}

据我所知,绑定甚至没有尝试获取数据,因为我尝试将转换器放在绑定上,并且 Convert 方法上的断点永远不会被命中.

From what I can tell, the binding is not even attempting to get the data as I've tried putting a converter against the binding, and the breakpoint on the Convert method never gets hit.

我正在使用 MVVM 模式,因此窗口的 DataContext 属性设置为视图模型.

I'm using the MVVM pattern so the window's DataContext property is set to a view model.

我在这里阅读了一些其他问题,并尝试了各种绑定排列,例如:

I've read some other questions on here and tried various permutations of the binding such as:

<MyColumn ExtendedColumnData="{Binding DataContext.ColumnToolTipDescriptions, ElementName=MyViewName}" />
<MyColumn ExtendedColumnData="{Binding DataContext.ColumnToolTipDescriptions, RelativeSource={RelativeSource AncestorType={x:Type local:MyView}}" />

但仍然没有运气,绑定没有触发!烦人的是,这似乎工作正常(如果我将属性添加到网格中):

But still no luck, the binding doesn't fire! The annoying thing is, this seems to work fine (if I add the property to the grid):

<MyGrid ExtendedColumnData="{Binding ColumnToolTipDescriptions}">
    <MyColumn />
</MyGrid>

我对 WPF 没有那么丰富的经验,所以我确定我错过了什么?

I'm not that experienced with WPF so I'm sure I'm missing something?

推荐答案

问题是 MyColumnCollection 没有继承数据上下文(控件的通常属性不是继承上下文的一部分).如果没有数据上下文绑定将无法工作.

The problem is that MyColumnCollection is not inheriting data context (usual properties of a control are not part of inheritance context). If you don't have a data context bindings will not work.

要解决这个问题,请尝试不从 ObservableCollection 而是从 FreezableCollection 继承 MyColumnCollection(可冻结属性是继承上下文的一部分).

To fix that, try inheriting MyColumnCollection not from ObservableCollection, but from FreezableCollection (freezable properties are part of inheritance context).

这篇关于用户控件的子依赖对象上的绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)