I'm trying to bind to a property of MainWindow, but from a ContextMenu
within a DataTemplate
. How can I achieve this?
- I can't use
ElementName
, as the contextMenu isn't part of the visual tree I can't use
PlacementTarget
, as this will give theUIElement
produced by theDataTemplate
<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>