我想知道如何將MenuItem.Header綁定到父窗口/用戶控件依賴項屬性?下面是一個簡單的例子:如何將MenuItem.Header綁定到Window/UserControl依賴項屬性?
Window1.xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" x:Name="self">
<Grid>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="{Binding Path=MenuText, ElementName=self}" />
</ContextMenu>
</Grid.ContextMenu>
<TextBlock Text="{Binding Path=MenuText, ElementName=self}"/>
</Grid>
</Window>
Window1.xaml.cs:
public partial class Window1 : Window {
public static readonly DependencyProperty MenuTextProperty = DependencyProperty.Register(
"MenuText", typeof (string), typeof (Window1), new PropertyMetadata("Item 1"));
public Window1()
{
InitializeComponent();
}
public string MenuText {
get { return (string)this.GetValue(MenuTextProperty); }
set { this.SetValue(MenuTextProperty, value); }
}
}
在我的情況下,文本塊顯示 「項目1」,並上下文菜單顯示空的項目。我在做什麼錯了?在我看來,我面臨着嚴重誤導WPF數據綁定原則的問題。
請您澄清如何通過標記將引用傳遞給Window/UserControl?如果我使用Tag =「{x:Reference self}」語法,則會出現編譯錯誤**「標記'引用'在XML命名空間中不存在'http://schemas.microsoft.com/winfx/2006/xaml' 。**我使用VS2008和.NET框架3.5。 – 2011-04-26 06:04:53
只存在於.NET 4中,您應該可以使用Binding代替,如'Tag =「{Binding ElementName = self}」' – 2011-04-26 10:58:47
Clear.Thanks很多 – 2011-04-26 13:14:54