2010-11-25 129 views
0

我试图与它一<Menu>元素绑定到的DependencyProperty的窗口内容元素:势必财产

这是我的XAML:

<Window x:Class="attachement.xWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Menu/> 
     <ToolBarTray x:Name="ToolBarTray" Grid.Row="1"> 
     </ToolBarTray> 
     <ScrollViewer Grid.Row="2"> 

     </ScrollViewer> 
    </Grid> 
</Window> 

这里是我后面的代码:

public partial class xWindow : Window 
{ 

    public Menu Menu 
    { 
     get { return (Menu)GetValue(MenuProperty); } 
     set { SetValue(MenuProperty, value); } 
    } 
    public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(Menu), typeof(xWindow), new UIPropertyMetadata(0)); 

    public xWindow() 
    { 
     InitializeComponent(); 
    } 
} 

现在我的问题是:我怎么能<Menu>元素绑定在我的XAML的依赖属性在后面的代码,这样,当我做“myXwindow.Menu =新菜单(){.. };”菜单在窗口中更新?

感谢

注:我尝试设置这样的XAML:<Menu x:Name="Menu">,并删除C#中的DP所以taht我可以直接访问在XAML中定义的菜单,至极似乎工作(没有生成或运行错误),但不会让我重新设置窗口已经显示

回答

1

后,你可以用你的Menu其他一些控制

<ContentControl x:Name="_menuContainer"> 
    <Menu/> 
</ContentControl> 

,然后写你的财产是这样的:

public Menu Menu 
{ 
    get { return (Menu)_menuContainer.Content; } 
    set { _menuContainer.Content = value; } 
} 
+0

是的,我实际上已经这样做了,但是我希望有一个不涉及包装容器的解决方案(我是极简主义者)。好像虽然不能... – David 2010-11-25 10:59:45