WPF属性绑定源属性未被访问

时间:2014-11-24 13:07:40

标签: .net wpf xaml binding dependency-properties

我正在尝试将从MenuItem继承的控件的依赖项属性绑定到我的窗口的依赖项属性。我在两个属性的get和sets上都设置了断点,我从未看到被调用窗口的属性get。 window属性是源,control属性是目标。

控件的属性如下所示:

Public Shared StorageProperty As DependencyProperty = 
                                 DependencyProperty.Register("Storage",
                                                             GetType(IStorage),
                                                             GetType(MRUFileList),
                                                             New PropertyMetadata(Nothing))

Public Property Storage As IStorage
    Get
        Return DirectCast(GetValue(StorageProperty), IStorage)
    End Get
    Set(value As IStorage)
        SetValue(StorageProperty, value)
    End Set
End Property

窗口的属性是:

Public Shared ReadOnly MRUStorageProperty As DependencyProperty =
                                             DependencyProperty.Register("MRUStorage",
                                                                         GetType(MRU.IStorage),
                                                                         GetType(GrammarEditor),
                                                                         New PropertyMetadata(Nothing))

Public Property MRUStorage As MRU.IStorage
    Get
        Return DirectCast(GetValue(MRUStorageProperty), MRU.IStorage)
    End Get
    Set(value As MRU.IStorage)
        SetValue(MRUStorageProperty, value)
    End Set
End Property

最后,定义绑定的XAML是:

<mru:MRUFileList Name="mnuRecent" 
                 Header="Open _Recent" 
                 Storage="{Binding MRUStorage, 
                           Mode=TwoWay, 
                           UpdateSourceTrigger=PropertyChanged}" />

以前我曾尝试将RelativeSource设置为Self,也设置为将AncestoryType设置为Window的FindAncestor。

1 个答案:

答案 0 :(得分:1)

使用依赖项属性绑定时,将永远不会调用get / set,因为使用依赖项属性基础结构解析了绑定。正如您所看到的,所有属性都允许您通过调用

轻松访问该依赖项属性的值
GetValue(StorageProperty)

绑定引擎只是直接调用

,而不是使用你的属性

编辑:您的窗口是否将MRUStorage作为属性?您将需要它能够将其用作绑定路径