Add doubleclick-command to wpf-treeview-items - wpf

as I found a lot of tasks (questions) to my question, I still have no idea how that works.
Even it's a much too complex example or simply the needed namespace was missing. So after one hour of research, my question...
How do I intergrate doubleclick-command to my WPF treeview (-items)?
Actually, my tree looks like this:
<!-- used Namespace: xmlns:i="http://schemas.microsoft.com/xaml/behaviors" -->
<TreeView DataContext="{Binding ProjectTree}" ItemsSource="{Binding ProjectNode}" DockPanel.Dock="Left"
x:Name="ProjectTree" Margin="0 0 2 0" Grid.Column="0">
<TreeView.ContextMenu>
<ContextMenu StaysOpen="True">
<MenuItem Header="Löschen" Height="20"
Command="{Binding RemoveNodeCommand}"
CommandParameter="{Binding ElementName=ProjectTree, Path=SelectedItem}">
<MenuItem.Icon>
<Image Source="/Icons/32x32/Remove_32x32.png" Width="15" Height="15"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TreeView.ContextMenu>
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectedItemChanged">
<i:InvokeCommandAction Command="{Binding TreeNodeSelectedCommand}"
CommandParameter="{Binding ElementName=ProjectTree, Path=SelectedItem}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding UpdateNodeCommand}"
CommandParameter="{Binding ElementName=ProjectTree, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal">
<Image Margin="3" Source="{Binding ItemType, Converter={x:Static misc:TreeItemImageConverter.Instance }}" Width="20" />
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
I don't really understand how the Interaction.Triggers work but should they be the right way?
I'm using the ICommand and a relaycommand for my menu- and button-commands. So far, everything fine in my viewmodels.
Sorry if that is a noob question.
EDIT
As it wasn't a noob question but a noob typo, MouseDoubleClick simply works as SelectedItemChanged so my question was unneccessary. (updated code above)

You can add an interaction trigger in the data template.
I don't have your code so I've adapted a project I already had hanging about to show this working.
My markup, which is of course illustrative rather than cut and paste ready for your purposes:
<TreeView Name="tv" ItemsSource="{Binding Families}" Grid.Row="1" Grid.ColumnSpan="2"
>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Family}" ItemsSource="{Binding Members}">
<Border>
<StackPanel Orientation="Horizontal"
Height="32"
>
<Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Border>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:FamilyMember}">
<ContentControl>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding DataContext.TestCommand, RelativeSource={RelativeSource AncestorType=TreeView}}"
CommandParameter="{Binding Path=DataContext, RelativeSource={RelativeSource AncestorType=TreeViewItem}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel Orientation="Horizontal"
Height="32"
x:Name="sp">
<TextBlock Tag="{Binding DataContext.Name,
RelativeSource={RelativeSource AncestorType=TreeViewItem , AncestorLevel=2}
}"
Text="{Binding Name}" />
<TextBlock Text="{Binding Age}" Foreground="Green" />
</StackPanel>
</ContentControl>
</DataTemplate>
</TreeView.Resources>
</TreeView>
The command in my window viewmodel
public class MainWindowViewModel
{
private DelegateCommand<FamilyMember> testCommand;
public ICommand TestCommand
{
get
{
if (testCommand == null)
{
testCommand = new DelegateCommand<FamilyMember>((member) =>
{
MessageBox.Show($"Member's name is {member.Name}");
});
}
return testCommand;
}
}
My tree has family and then each has family members.
If I expand members out I can double click a family member and the message box confirms the name is correct.
Note the contentcontrol in the datatemplate.
You need a control to give you double click support.

Interaction triggers cannot be applied in style setters but you could create an attached behaviour that handles the MouseDoubleClick event for the TreeViewItem:
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="local:YourBehavior.Command" Value="{Binding YourCommand}" />
...
</Style>
Another option is to invoke the command programmtically from the code-behind of the view.

Related

How to add CheckBox content from resource file in wpf?

I have a combobox in which first item is checkbox to select all entries from below drop down list. With hard coded string its working fine but now I want to localize it...want to set string from resource file but as its deeply inserted couldn't add it. For that I tried adding panel and one checkbox along with textblock also simply separated textblock from checkbox.
This is code :
<ComboBox Name="CmbEntries" IsTabStop="{Binding CurrentDialogViewModel, Converter={StaticResource NullToBoolConverter}}" Grid.Column="1" Grid.Row="4" VerticalAlignment="Top" Margin="2,0,0,5" SelectedItem="{Binding SelectedEntries}" SelectedIndex="0" HorizontalAlignment="Left" Width="400">
<ComboBox.Resources>
<CollectionViewSource x:Key="EntriesCollection" Source="{Binding DicomDir.Entries}"/>
</ComboBox.Resources>
<ComboBox.ItemsSource>
<CompositeCollection>
<ComboBoxItem>
<CheckBox x:Name="all" Margin="8,0,0,0"
IsChecked="{Binding IsAllEntriessSelected, Mode=TwoWay}"
Command="{Binding SelectAllEntriessCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
**Content="Select All Entries"** />
</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource EntriesCollection}}"/>
</CompositeCollection>
</ComboBox.ItemsSource>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Margin="8,0,0,0"
IsChecked="{ Binding Path=IsAnyEntitySelected, Mode=TwoWay }"
Command="{Binding SelectAllEntityCommand}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />
<TextBlock Margin="5,0,0,0" TextWrapping="Wrap" Text="{Binding DisplayEntriesInfo}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding HasSelectedEntities}" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
Scrshot1-Expected
Scrshot2-Actual with Resx
In this case, is it possible to read it directly from resource file?
To bind the content to a resource you do:
<Window ...
xmlns:properties="clr-namespace:YourNamespace.Properties">
<Grid>
<CheckBox Content="{x:Static properties:Resources.Foo}" />
</Grid>
</Window>
For this to work you need Resources to be public.
If it is not you get:
Provide value on 'System.Windows.Markup.StaticExtension' threw an exception...
StaticExtension value cannot be resolved to an enumeration, static field, or static property.

Bind a command to a specific event on an Item of ItemsControl (MVVM)

I would like to bind the invoke of a command when an user double clicks on an Item of my ItemsControl.
So I used System.Windows.Interactivity (v3.5):
<ItemsControl ItemsSource="{Binding AllFiles, Mode=OneWay}">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type viewModel:FolderVM}">
<DockPanel Margin="5" Height="70" Width="300">
<DockPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
</DockPanel.Resources>
<i:Interaction.Triggers>
<!--Exception:Cannot add content of type 'System.Windows.Interactivity.EventTrigger' to an object of type 'System.Windows.Interactivity.TriggerCollection'.-->
<i:EventTrigger EventName="MouseDown">
<i:InvokeCommandAction CommandName="CmdBrowseFolder"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Image Height="70" Width="70" DockPanel.Dock="Left" Source="{Binding Icon}"/>
<Grid Background="LightBlue" DockPanel.Dock="Left">
<WrapPanel Margin="10,0,0,0" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="Folder"/>
</WrapPanel>
</Grid>
</DockPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:FileVM}">
<DockPanel Margin="5" Height="70" Width="300">
<DockPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextWrapping" Value="WrapWithOverflow"/>
</Style>
</DockPanel.Resources>
<Image Height="70" Width="70" DockPanel.Dock="Left" Source="{Binding Icon}"/>
<Grid Background="LightBlue" DockPanel.Dock="Left">
<WrapPanel Margin="10,0,0,0" Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding SizeFormatted}"/>
</WrapPanel>
</Grid>
</DockPanel>
</DataTemplate>
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
But the following exception triggers as soon as I run the application: Cannot add content of type 'System.Windows.Interactivity.EventTrigger' to an object of type 'System.Windows.Interactivity.TriggerCollection'.
Edit: Some more information:
when I comment the line with InvokeCommandAction no exception anymore. But of course the event doesn't trigger the command
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<!--<i:InvokeCommandAction CommandName="CmdBrowseFolder"/>-->
</i:EventTrigger>
</i:Interaction.Triggers>
Another information: InvokeCommandAction in version 3.5 doesn't contain Command DP, only CommandName and CommandParameter
Seems like there's no solution for the version 3.5.
Bind Events to Commands
I don't know why there's CommandName and CommandParameter DP if we can't use them.

Wpf Treeview Contextmenu DataContext

I am trying to set the Datacontext of my Contextmenu - but my Code does not work. Very similar code works at another location, so I would be grateful if somebody could explain why it does not work.
My Treeview looks like this:
Beware: it is rather long, but I think the nested structure is part of the problem, so I do want to shrink it.
The Problem occurs on the second "level" at the ppChart Binding.
<TreeView x:Name ="Presentation_SlidesWithIndex" Grid.ColumnSpan="1" HorizontalAlignment="stretch" Height="auto" Margin="0,0,3,0" VerticalAlignment="stretch" Width="auto" Tag="{Binding DataContext, ElementName=LayoutRoot}">
<TreeView.ItemContainerStyle>
<!--expand Charts when they contain elements-->
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding HasCharts}"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type pp:PPSlide}" ItemsSource="{Binding Charts}">
<StackPanel x:Name="PPSlideElements" Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<!--Trigger for Slides with Charts-->
<DataTrigger Binding="{Binding Path=HasCharts}" Value="True" >
<Setter Property="Background" Value="LightBlue" />
</DataTrigger>
<!--Trigger for Slides with NO Charts-->
<DataTrigger Binding="{Binding Path=HasCharts}" Value="False" >
<Setter Property="Opacity" Value=".5" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<TextBlock FontSize="15">
<Run Text="Slide "></Run>
<Run Text="{Binding Path=Index}"></Run>
</TextBlock>
<!--<Image Source="/Images/pptIcon.png" Height="10"></Image>-->
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type pp:PPChart}" ItemsSource="{Binding ExcelSource}">
<StackPanel x:Name="PpChartElements" Orientation="Horizontal" AllowDrop="True" cal:Message.Attach="[Event Drop] = [Action DropItem($eventArgs,$view)]" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Style.Triggers>
<!--Trigger for Charts with DataSource set-->
<DataTrigger Binding="{Binding Path=HasDataSourceSet}" Value="True" >
<Setter Property="Background" Value="LightGreen" />
</DataTrigger>
<!--Trigger for Charts with DataSource nit set-->
<DataTrigger Binding="{Binding Path=HasDataSourceSet}" Value="False" >
<Setter Property="Background" Value="Orange" />
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<Image Source="{Binding ChartType, Converter={StaticResource PowerPointChartTypeConverter}}" Width="19" Height="19" Margin="2,2,4,2"></Image>
<TextBlock FontSize="14" Text="{Binding Path=ShapeName}" VerticalAlignment="Center">
<TextBlock.ContextMenu>
<ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag.AddEntityCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
<MenuItem Header="Löse Verknüpfung" cal:Message.Attach="DeleteLink($datacontext)" ToolTip="Löscht einen bestehenden Link zu einer Excel Datei"/>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
<Image Source="{Binding PowerPointWriteStatus, Converter={StaticResource PowerPointWriteStatusConverter}}" Width="19" Height="19" Margin="2,2,4,2" ToolTip="Zeigt erfolg oder misserfolg des Erstellens an"></Image>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type pp:PPSourceExcelLink}">
<StackPanel x:Name="PpSourceExcelLinkStackPanel" Orientation="Vertical">
<TextBlock FontSize="14">
<Run Text="Datei: "></Run>
<Run Text="{Binding Path=ExcelFileName, Mode=OneWay}"></Run>
</TextBlock>
<TextBlock FontSize="14">
<Run Text="Tabelle: "></Run>
<Run Text="{Binding Path=SourceTableID}"></Run>
</TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
I think your problem lies exactly in this line of code:
cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag.AddEntityCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
To fix it you need to change this to:
cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget.Tag.AddEntityCommand, RelativeSource={RelativeSource Self}}"
but this alone won't fix your problem because the target of the action message will be the TextBlock which doesn't have its Tag property set.
Anyway you should end up with code similar to this:
<TextBlock FontSize="14" Text="{Binding Path=ShapeName}" VerticalAlignment="Center" Tag="{Binding DataContext, RelativeSource={RelativeSource Self}}">
<TextBlock.ContextMenu>
<ContextMenu cal:Action.TargetWithoutContext="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}">
<MenuItem Header="Löse Verknüpfung" cal:Message.Attach="DeleteLink($datacontext)" ToolTip="Löscht einen bestehenden Link zu einer Excel Datei" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
I answered a similar question recently and you can take a look at for more details.
I found a Solution which works fairly well - but why it works is beyond me:
<TextBlock FontSize="14" Text="{Binding Path=ShapeName}" VerticalAlignment="Center" Tag="{Binding DataContext, RelativeSource={AncestorType=TreeView}}">
<TextBlock.ContextMenu>
<ContextMenu cal:Action.TargetWithoutContext="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}">
<MenuItem Header="Löse Verknüpfung" cal:Message.Attach="DeleteLink($datacontext)" ToolTip="Löscht einen bestehenden Link zu einer Excel Datei" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
Note how the Ancestertype treeview is used. my bet guess is that it walks the Visual tree until it finds the Treeview, which has the proper DataContect.
Thanks to Sniffer, which did a very good job !
As stated in RelativeSource binding from a ToolTip or ContextMenu there is no need to add the tag to the PlacementTarget. This makes it shorter, as there is no need to add a tag to the control and to the binding path of the ContextMenu, e.g.:
<TextBlock FontSize="14" Text="{Binding Path=ShapeName}" VerticalAlignment="Center">
<TextBlock.ContextMenu>
<ContextMenu cal:Action.TargetWithoutContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Löse Verknüpfung" cal:Message.Attach="DeleteLink($datacontext)" ToolTip="Löscht einen bestehenden Link zu einer Excel Datei" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
and in the ViewModel
public void DeleteLink(object parameter)
{
if (parameter == null)
return;
var param = parameter as YourDataContextType;
if (! (param is YourDataContextType))
return;
// your delete handling
}

Binding a command defined in a datatemplate

I know there are few answers on this topic. But none of them was working in my case.
I have a ListView with a style and an ItemContainerStyle. In the ItemContainer Style, I define some triggers in order to use a different DataTemplate depending if the item in the list is selected or not. Then, finally in the Datatemplate I have a context menu with a command. The problem is how to bind the command to the viewmodel.
This is the ListView:
<ListView
x:Name="lstPersons"
Grid.Row="1"
Style="{StaticResource ListViewStyle}"
ItemContainerStyle="{StaticResource ItemContainerStyle}"
DataContext="{Binding}"
ItemsSource="{Binding Path=Persons}"
Tag="{Binding}"
SelectedItem="{Binding Path=SelectedPerson, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</ListView>
and these are the styles, datatemplates and contextmenu (defined in a resource dictionary).
The commands in the context menu do not work....:
<ContextMenu x:Key="SelectedItemContextMenu">
<MenuItem
Header="Do Something"
Command="{Binding Path=DataContext.DoSomethingCmd, ElementName=LayoutRoot}">
</MenuItem>
<MenuItem
Header="Do Something"
Command="{Binding PlacementTarget.Tag.DoSomethingCmd, RelativeSource={RelativeSource AncestorType=ContextMenu}}">
</MenuItem>
</ContextMenu>
<DataTemplate
x:Key="ItemTemplate">
<Canvas
Margin="4"
Width="60"
Height="60"
Background="LightGray">
<TextBlock
Foreground="Black"
Margin="2 0 0 0"
Opacity="0.5"
FontFamily="Segoe UI"
Text="{Binding Path=FirstName}" />
</Canvas>
</DataTemplate>
<DataTemplate
x:Key="ItemSelectedTemplate">
<Grid>
<Border
BorderBrush="Black"
BorderThickness="1"
Margin="3"
ContextMenu="{DynamicResource SelectedItemContextMenu}">
<Canvas
Width="60"
Height="60"
Background="LightBlue">
<TextBlock
Foreground="Black"
Margin="2 0 0 0"
Opacity="0.5"
FontFamily="Segoe UI"
Text="{Binding Path=FirstName}" />
</Canvas>
</Border>
</Grid>
</DataTemplate>
<!--style of the listviewitem-->
<Style
TargetType="{x:Type ListViewItem}"
x:Key="ItemContainerStyle">
<Setter
Property="ContentTemplate"
Value="{StaticResource ItemTemplate}" />
<Style.Triggers>
<Trigger
Property="IsSelected"
Value="True">
<Setter
Property="ContentTemplate"
Value="{StaticResource ItemSelectedTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
<!--style of the listview-->
<Style
TargetType="{x:Type ListBox}"
x:Key="ListViewStyle">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type ListBox}">
<Grid>
<Border>
<ScrollViewer
Focusable="false">
<WrapPanel
IsItemsHost="True"
Orientation="Horizontal"
Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"/>
</ScrollViewer>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Your ContextMenu is used inside a data template. I will be put in a different name scope of "LayoutRoot" and ElementName binding won't work. Also, the PlacementTarget of your context menu is the Border, and you've not setup any Tag on it. So the second command won't work either.
It looks like you are implement the commands on the ListBox level (or LayoutRoot?). It might be easier to put your context menu on the ListBox, and use ListBox.SelectedItem to find the current selection and apply your logic on it.
You can use RelativeSource:
<ContextMenu x:Key="SelectedItemContextMenu">
<MenuItem
Header="Do Something"
Command="{Binding Path=DataContext.DoSomethingCmd, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}">
</MenuItem>
</ContextMenu>
You should probably be using RoutedCommands instead of VM commands in this case. You would bind the RoutedCommand to the ContextMenu, and since you only need static object references for that, finding them shouldn't be a problem. Then you'd set up appropriate CommandBindings on the controls that should handle the commands (either ListView or ListViewItem, depending on whether you want the List-ViewModel or the Item-ViewModel to handle the command). These controls will know their ViewModels, so binding to them will not be a problem there. Through the process of Command Routing, which is built-in in WPF, the context menu will find the proper target for its command automatically.
For guidance on how to set up CommandBindings in a MVVM-friendly way, you might want to refer to http://wpfglue.wordpress.com/2012/05/07/commanding-binding-controls-to-methods/

How add command to an ElementMenuItem?

I am trying to add a command to an ElementMenuItem, but the command can not be fired.
<Grid Name="MenuGrid">
<s:ElementMenu
Name="MainMenu"
ActivationMode="AlwaysActive"
ActivationHost="{Binding ElementName=MenuGrid}"
ItemsSource="{Binding Menu}">
<s:ElementMenu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Menu}" DataType="{x:Type s:ElementMenuItem}">
<Grid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<behaviours:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=NavigatorMenu.SimpleCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Image Source="{Binding ImageUri}"></Image>
<TextBlock Text="{Binding Title}"
VerticalAlignment="Bottom"
HorizontalAlignment="Center">
</TextBlock>
</Grid>
</HierarchicalDataTemplate>
</s:ElementMenu.ItemTemplate>
</s:ElementMenu>
</Grid>
Does anyone know how to add a command to the menu? Thank you in advance.
No idea what those controls are but if they inherit from normal menus you should use the ItemContainerStyle to hook up commands:
<s:ElementMenu Name="MainMenu" ActivationMode="AlwaysActive"
ActivationHost="{Binding ElementName=MenuGrid}" ItemsSource="{Binding Menu}">
<s:ElementMenu.ItemContainerStyle>
<Style TargetType="s:ElementMenuItem">
<Setter Property="Command"
Value="{Binding Source={StaticResource Locator}, Path=NavigatorMenu.SimpleCommand}" />
</Style>
</s:ElementMenu.ItemContainerStyle>
<s:ElementMenu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Menu}">
<Grid>
<Image Source="{Binding ImageUri}"/>
<TextBlock Text="{Binding Title}" VerticalAlignment="Bottom"
HorizontalAlignment="Center"/>
</Grid>
</HierarchicalDataTemplate>
</s:ElementMenu.ItemTemplate>
</s:ElementMenu>
This of course assumes that all the bindings actually work, if they don't you should probably debug them...
I don't know why but I find interaction triggers don't work in a Grid. Change your xaml to
<HierarchicalDataTemplate ItemsSource="{Binding Menu}" DataType="{x:Type s:ElementMenuItem}">
<ContentControl>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<behaviours:EventToCommand Command="{Binding Source={StaticResource Locator}, Path=NavigatorMenu.SimpleCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Image Source="{Binding ImageUri}"></Image>
<TextBlock Text="{Binding Title}"
VerticalAlignment="Bottom"
HorizontalAlignment="Center">
</TextBlock>
</Grid>
</ContentControl>
</HierarchicalDataTemplate>

Resources