分配值后,User Control中的依赖属性为null

时间:2010-09-01 13:02:43

标签: c# .net wpf dependency-properties

我有一个用户控件,有一个Frame作为依赖属性

public partial class NavigationBar : UserControl
{
    public NavigationBar()
    {
        this.InitializeComponent();
    }

    public Frame NavigationFrame
    {
        get { return (Frame)GetValue(NavigationFrameProperty); }
        set { SetValue(NavigationFrameProperty, value); }
    }

    // Using a DependencyProperty as the backing store for NavigationFrame.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NavigationFrameProperty =
        DependencyProperty.Register("NavigationFrame", typeof(Frame), typeof(NavigationBar), new UIPropertyMetadata());
}

使用用户控制代码:

<userControls:NavigationBar NavigationFrame="{Binding ElementName=masterPage, Path=frameNavigations}" />

 <Frame x:Name="frameNavigations"/>

为什么NavigationFrame依赖属性在加载后仍为null?

提前致谢

1 个答案:

答案 0 :(得分:2)

这将在页面中查找名称为masterPage的元素,然后在该对象上查找名为frameNavigations的属性。我认为你实际上想要绑定名为frameNavigations的元素,所以你只需要写:

<userControls:NavigationBar
    NavigationFrame="{Binding ElementName=frameNavigations}"/>
<Frame x:Name="frameNavigations"/>