TriggerAction on databound menus

TriggerAction on databound menus

Old forum URL:

dan_morey posted on Thursday, August 18, 2011

I have a readonly list of TaskGroups that contains a description and a readonly list of Tasks that also contain a description.

I've bound a MenuItem to the list of Task and I'm using HierarchicalDataTemplates to display the TaskGroup Description and Task Description in the menu. So the TaskGroups appear in the first dropdown and the Tasks appear when one of the task groups is hovered over.

The problem I now have is that I have no idea which task has been clicked on. In the XAML only the root item is available and if I use a TriggerAction on that, I get the click event, but don't now which task was clicked on. I've tried adding a TriggerAction into the HierarchicalDataTemplate and linking it to the TextBlock I have in there but this doesn't seem to work, probably as it's the menuitem getting clicked and not the textblock.

I think I could do this with commanding but I was trying to avoid that in case I ever needed to use more than the click event.

Does anyone know if this is possible to do with the TriggerAction, or do I need to look elsewhere (and if so where)?



dan_morey replied on Friday, September 02, 2011

   I've found out how to do this. Here is my code:

        <Menu Grid.Column="0" Grid.Row="0" Background="White">


                <HierarchicalDataTemplate DataType="{x:Type library:TaskInfo}">


                        <TextBlock Text="{Binding DisplayName}" />


                            TargetControl="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"


                            DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}"


                            MethodParameter="{Binding DataContext.DisplayName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"




                <HierarchicalDataTemplate DataType="{x:Type library:TaskGroupInfo}" ItemsSource="{Binding Path=Tasks}">


                        <TextBlock Text="{Binding DisplayName}" />




            <MenuItem x:Name ="MainMenuItem" ItemsSource="{Binding TaskGroups}"



                    <Image Source="/BxfTest;component/Images/logo.gif" />




 The TriggerAction goes into the DataTemplate for the menuitem, and links to the menuitem by using FindAncestor.

Copyright (c) Marimer LLC