I have a menu in WPF that I'm trying to build dynamically using bindings and a HierarchicalDataTemplate:
<Menu ItemsSource="{Binding MenuItems}">
<Menu.ItemContainerStyle>
<Style>
<Setter Property="cal:Message.Attach" Value="Click()" />
</Style>
</Menu.ItemContainerStyle>
<Menu.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding MenuItems}">
<TextBlock Text="{Binding Name}" cal:Message.Attach="Click()" Background="Transparent"/>
</HierarchicalDataTemplate>
</Menu.ItemTemplate>
</Menu>
An IMenuItem
has a Click
method, a collection of 'sub' MenuItems
, and a Name
displayed in the text block.
It all worked fine until I tried to get the Click
to work. Since the click event is fired on the MenuItem
created by the template, I don't have access to it to bind my Caliburn action with cal:Message.Attach="Click()"
.
I tried adding the action by using a setter in the ItemContainerStyle.
It seemed to work, however I'm getting click events firing in multiple places:
For example, if I click on "Test 2":
The click event on both "Test 2" model and the "Data Manager" model are fired.