2010-11-25 84 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