I have a listbox and its data template. Items of listbox are binded with a static source. In data template I have a button, and created context menu of it and some menu items.
<ObjectDataProvider x:Key="GlobalUnits"/>
<DataTemplate x:Key="listboxTemplate" DataType="UnitClass">
<StackPanel Margin="3" >
<DockPanel >
<Button DockPanel.Dock="Left" Margin="5,0,10,0">
<Button.ContextMenu>
<ContextMenu x:Name="CMUnits" Opened="OnContextMenuOpened"
MenuItem.Click="OnMenuItemClick">
<MenuItem Header="Add"
x:Name="MenuItemName" />
<MenuItem Header="Delete"
x:Name="MenuItemDelete" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
</StackPanel>
</DataTemplate>
<ListBox x:Name="TUListBox"
local:DragDropManager.DragSourceAdvisor="{StaticResource sourceAdvisor}"
ItemTemplate="{DynamicResource listboxTemplate}"
ItemsSource="{Binding Source={StaticResource GlobalUnits}}"/>
In Code behind:
void OnMenuItemClick(object sender, RoutedEventArgs e)
{
if (e.Source == this.MenuItemDelete) <//error here MenuItemDelete is not available
{
}
}
I have defined MenuItemDelete in datatemplate for a button as menuitem. Any suggestion, if I put context menu out of datatemplate in normal xaml code it works.
Well my program debug cursor does not even reach to OnMenuItemClick event when i click on menu item in runtime
Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…