我正在尝试将一个包含<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();
}
}现在我的问题是:如何将xaml中的<Menu>元素绑定到后台代码中的dependency属性,以便在执行"myXwindow.Menu = new Menu(){...};“时在窗口中更新菜单?
谢谢
注:我尝试这样设置xaml:<Menu x:Name="Menu">并在c#中删除dp,这样我就可以直接访问在xaml中定义的菜单,该菜单似乎可以工作(没有构建或运行错误),但不允许我在窗口显示后重新设置它
发布于 2010-11-25 18:56:25
您可以将Menu包装在其他一些控件中
<ContentControl x:Name="_menuContainer">
<Menu/>
</ContentControl>然后像这样写你的属性:
public Menu Menu
{
get { return (Menu)_menuContainer.Content; }
set { _menuContainer.Content = value; }
}https://stackoverflow.com/questions/4275829
复制相似问题