Reputation: 6124
I'm trying to have a window with a <Menu>
element in it bound to a dependencyProperty:
Here is my 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>
and here is my code behind:
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();
}
}
now my question is: how can I bind the <Menu>
element in my xaml to the dependency property in code behind so that when I do "myXwindow.Menu = new Menu(){...};" the menu is updated in the window?
thanks
NB: I tried setting the xaml like this : <Menu x:Name="Menu">
and remove the dp in c# so taht I could access directly the Menu defined in xaml, wich seems to work (no build or run error) but does not allow me to set it anew after the window has been displayed
Upvotes: 0
Views: 190
Reputation: 5003
You can wrap your Menu
in some other control
<ContentControl x:Name="_menuContainer">
<Menu/>
</ContentControl>
And then write your property like this:
public Menu Menu
{
get { return (Menu)_menuContainer.Content; }
set { _menuContainer.Content = value; }
}
Upvotes: 1