TriggerAction on databound menus

TriggerAction on databound menus

Old forum URL: forums.lhotka.net/forums/t/10625.aspx


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)?

Thanks.

Dan.

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">

            <Menu.Resources>

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

                    <StackPanel>

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

                        <csla:TriggerAction

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

                            TriggerEvent="Click"

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

                            MethodName="TaskSelect"

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

                        />

                    </StackPanel>

                </HierarchicalDataTemplate>

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

                    <StackPanel>

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

                    </StackPanel>

                </HierarchicalDataTemplate>

            </Menu.Resources>

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

               Background="White">

                <MenuItem.Icon>

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

                </MenuItem.Icon>

            </MenuItem>

        </Menu>

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

Copyright (c) Marimer LLC