Suppose I have a context menu as a window resource:
<Window.Resources>
<ContextMenu
x:Shared="false"
x:Key="ContextMenu">
<MenuItem Header="{??}" />
</ContextMenu>
</Window.Resources>
And I want to bind MenuItem.Header to a dependency property called MenuItemHeader on the window. What should {??} be and what should the code behind in the window class look like?
You could name the window and reference it
<Window Name="Window" ...>
<MenuItem Header="{Binding MyDp, Source={x:Reference Window}}"...>
MyDp being the property. If you don't know how to define a dependency property read the overview.
(I would use Source and x:Reference as ElementName usually does not work in disconnected places like ContextMenus)
Related
Because ToolbarTray does not support proper ItemsSource binding I'm doing things in code behind. Namely filling the tray. How can I tell which toolbar the contextmenu was opened for (with CommandParameter)?
<UserControl.Resources>
<ContextMenu x:Key="ToolbarContextMenu">
<MenuItem Header="Move to top" Command="{ui:CommandHandler MoveToTop}" />
<MenuItem Header="Move to left" Command="{ui:CommandHandler MoveToLeft}" />
<MenuItem Header="Make float" Command="{ui:CommandHandler MakeFloat}" />
</ContextMenu>
</UserControl.Resources>
<ToolBarTray x:Name="MainToolbarTray" Orientation="{Binding Orientation}" ContextMenu="{StaticResource ToolbarContextMenu}"/>
In example above I use a toolkit where commands in ViewModel are decorated with attribute and in xaml command can be defined like this.
(Not the same as suggested. I don't want to find ToolbarTray, but the Toolbar)
EDIT: I just assigned the ContextMenu to Toolbar in code behind and used PlacementTarget as suggested.
I am using the Command pattern with (among other things) a context menu on a TreeViewItem, which uses HierarchicalDataTemplates. The MainWindowViewModel (a static resource for the Window) has properties that expose singleton objects that in turn have properties that represent the commands. I am able to execute the command just fine, but some of the commands need to pass the TreeViewItem's DataContext as the CommandParameter.
Here's a specific example:
One node of the tree has the ItemsSource bound to an ObservableCollection of individual AnalysisMain objects. Each of the resulting subnodes has a ContextMenu (with a DataContext bound to the AnalysisController) which has (among others) a Remove MenuItem. The Remove MenuItem's Command property is bound to the CommandRemove command on the AnalysisController singleton object (and it executes just fine). But this also requires the CommandParameter to be bound to the AnalysisMain object that serves as the DataContext for the subnodes in the tree. I have tried using RelativeSource with the AncestorType set to TreeViewItem and the Path set to DataContext:
<HierarchicalDataTemplate DataType="{x:Type vmAnalysis:AnalysisMain}">
<WrapPanel Orientation="Horizontal">
<Image Source="Analysis\Icon_Analysis_Main_16_Normal.png" Margin="0,0,2,0" Width="16"/>
<TextBlock Text="{Binding TreeViewTitle}">
<TextBlock.ContextMenu>
<ContextMenu DataContext="{StaticResource mainWindowViewModel}">
<MenuItem Header="Remove" Command="{Binding Path=AnalysisController.CommandRemove}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}, AncestorLevel=4}, Path=DataContext}">
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</WrapPanel>
</HierarchicalDataTemplate>
Without the AncestorLevel set, when I open the ContextMenu, I get the following in the Output window:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.TreeViewItem', AncestorLevel='4''. BindingExpression:Path=DataContext; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'CommandParameter' (type 'Object')
I have tried several values for the AncestorLevel to no avail.
From examining the Visual Tree in Christian Mosers WPF Inspector, I don't see the context menu in the visual tree. Although the TreeViewItem shows the DataContext object. I just need a way to "navigate" to it in order to bind to it.
As an alternative, I have tried leaving the ContextMenu DataContext alone and setting the Command Binding's Source to point back to the AnalysisController. This also works for executing the command, but I am not able to bind to the TreeViewItem's DataContext for the CommandParameter:
<ContextMenu>
<MenuItem Header="Remove" Command="{Binding Source={StaticResource mainWindowViewModel}, Path=AnalysisController.CommandRemove}"
CommandParameter="{Binding Source={RelativeSource Self}, Path=DataContext}">
</MenuItem>
</ContextMenu>
I have also tried just using CommandParameter="{Binding}", which also doesn't work. (In both cases, I just get null sent as the parameter. No warning / error is written to the Output window.) EDIT: For anyone else with this problem, the second option was doomed from the beginning, because I mistakenly put in Source={RelativeSource Self}, which would refer to the MenuItem and not the TreeViewItem. However, changing this with AncestorType / AncestorLevel makes no difference.
You have to use PlacementTarget property of the ContextMenu
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu
DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
</ContextMenu>
</Setter.Value>
</Setter>
And on your MenuItem do a RelativeSource to ContextMenu then use PlacementTarget.DataContext as your binding to your CommandParameter
I would like to reuse a ContextMenu on several DataGrid.
So I placed the context menu in the Resources of my Window.
I have trouble to bind to the SelectedItem property of the DataGrid on which the ContextMenu is placed.
In this example, I'm trying to have the Name property of the SelectedItem displayed in the context menu.
<Window.Resources>
<ContextMenu x:Key="DgContextMenu"
DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
<MenuItem Header="{Binding SelectedItem.Name, RelativeSource={RelativeSource AncestorType=ContextMenu}}" />
</ContextMenu>
</Window.Resources>
<DataGrid ItemsSource="{Binding CollectionView}"
ContextMenu="{StaticResource DgContextMenu}"
Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
Thanks in advance
The way you have written your example has binding error and that's why your context menu doesn't work. You have binded menu item header to SelectedItem.Name of ContextMenu object which doesn't have SelectedItem property (you can tell that from RelativeSource part of the menu item binding). One possible solution, among others, would be to bind DataContext of ContextMenu to DataGrid through PlacementTarget (not PlacementTarget.Tag). Since child controls „inherit“ DataContext of the parent you can just specify Path in the menu item binding. This is how it would look:
<Window.Resources>
<ContextMenu x:Key="DgContextMenu"
DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<MenuItem Header="{Binding Path=SelectedItem.Name}" />
</ContextMenu>
</Window.Resources>
<DataGrid ItemsSource="{Binding CollectionView}"
ContextMenu="{StaticResource DgContextMenu}"
>
</DataGrid>
Basically you can find those errors if you run application in VS debugger and watch output in Output window (Debug -> Window -> Output). In output window you should look for System.Windows.Data Error line and in that line you will see the type of an object and property you are trying to bind and that will give you a clue what's wrong with your binding in XAML.
I have a simple task to bind a context menu to items in a TreeView. If I define the context menu statically no problem. But if I want to bind the context menu to my "root" data context things start to break. So far I was not able to find a correct way to bind my Context menu to the original DataContext the TreeView uses.
What binding magic is need to get this working?
This Binding does not work:
<MenuItem Name="Name" Header="{Binding Path=DataContext.ContextMenuName, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}" />
Below is the full sample:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:ui="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView ItemsSource="{Binding Path=Persons, Mode=OneTime}" Name="cTree">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate>
<TextBox Text="{Binding Mode=OneWay}">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Name="Name" Header="{Binding Path=DataContext.ContextMenuName, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeView}}}" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
public partial class MainWindow : Window
{
public string[] Persons
{
get { return new string[] { "Alois", "Kraus", "Joe", "xxxx" }; }
}
public string ContextMenuName
{
get;
set;
}
public MainWindow()
{
ContextMenuName = "This is the data bound menu name";
InitializeComponent();
this.DataContext = this;
}
}
Basically I want to bind the property ContextMenuName (actually a command in my ViewModel) of the main form. Since the TreeView is rebinding its childs to the Persons (strings to keep it simple) I cannot get the root DataContext out of it. So far I was never able to find any Ancestor (TreeView or MainWindow) which should solve the issue? What am I doing wrong here?
ContextMenu doesn't lie in same visual tree as that of control on which it is applied. So FindAncestor won't be able to get to TreeView since it is not ancestor of ContextMenu.
Also ElementName won't work here since it also need two controls to be in same Visual tree.
You can use x:Reference which allows to bind even if they don't lie in same visual tree:
<MenuItem Name="Name"
Header="{Binding Path=DataContext.ContextMenuName,
Source={x:Reference cTree}}" />
Have you tried binding via ElementName=cTree instead of RelativeSource={...}?
I am using an ObjectDataProvider and a DataTemplate to populate a MenuItem inside my Menu bar. (WPF, C#/XAML) See snipet below.
Result: The top menu item appears, when i click on it, the wrapping menu item (the one with the bound header text) appears along with the little arrow indicating the presence of children but hovering or clicking the arrow does not show the children, they cannot be accessed.
Expected result: The children are visible and behave properly.
Snippet:
<ObjectDataProvider x:Key="Brokers" ObjectInstance="{x:Static brokers:BrokerManager.Instance}" MethodName="GetBrokers" IsAsynchronous="True" />
<DataTemplate x:Key="BrokerMenuItem" DataType="IBroker">
<MenuItem Header="{Binding Path=Name}">
<MenuItem Header="Connect" />
<MenuItem Header="Disconnect" />
</MenuItem>
</DataTemplate>
<MenuItem Header="Brokers" ItemsSource="{Binding Source={StaticResource Brokers}}" ItemTemplate="{DynamicResource BrokerMenuItem}"/>
arsenmrkt: I have exactly the same problem, if I populate a MenuItem using a DataTemplate I cant seem to add children to any of those generated items. I don't understand your answer though, how should I use the ContentPresenter to get around this problem?
EDIT:
Actually, my problem was'nt exactly the same, since I'm trying to bind a collection of collections to a menu. I think I've gotten it to work though using the HierarchicalDataTemplate:
<Menu>
<MenuItem Header="{Binding Name}" ItemsSource="{Binding MenuOptions}">
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Categories}">
<MenuItem Header="{Binding Name}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</Menu>
Does this help you NicholasF?
After searching for over a week, i finally found how to make this work properly. It turns out DataTemplates don't work too great for dynamic menus. The proper way to do this is to use the ItemContainerStyle property of MenuItem. (Or is that ItemStyleContainer?)
Simply create a style to override the header and set it to whatever you need. I them overrode the ItemsSource to include my children. However be careful here, as the children will inherit the style and each have the same children and generate a recursive menu. You'll need to override the ItemsSource of your children and set it to an empty x:Array or the likes.
There are several blogs out there describing how to use ItemContainerStyle, check them out.
ItemSource property of menuitem control is used for giving childes for that item, try to use <ContentPresenter /> with that datatemplate.