代码之家  ›  专栏  ›  技术社区  ›  Homam

赋值后,用户控件中的依赖项属性为空

  •  0
  • Homam  · 技术社区  · 15 年前

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

    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"/>
    

    提前谢谢

    1 回复  |  直到 15 年前
        1
  •  2
  •   Quartermeister    15 年前

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

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