我试图绑定到MainWindow的属性,但是从ContextMenu
内的DataTemplate
。我怎样才能做到这一点?如何绑定到ItemTemplate中的父级UIElement?
- 我不能使用
ElementName
,作为文本菜单不是视觉树 我不能使用
PlacementTarget
的一部分,因为这将得到一种由DataTemplate
产生的<Window x:Class="WpfApplication24.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ItemsControl ItemsSource="{Binding Data}"> <ItemsControl.ItemTemplate> <DataTemplate> <Border Padding="5" CornerRadius="10" BorderThickness="1" BorderBrush="Red"> <Border.ContextMenu> <ContextMenu ItemsSource="{Binding <I want to bind to a property of MainWindow here>}"/> </Border.ContextMenu> <TextBlock Text="{Binding}"/> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>
UIElement