Reputation: 41
I have this class:
public class LeftClickButton: Button
public LeftClickButton()
{
this.Click += LeftClickButton_Click;
}
I want the LeftClickButton_Click function to raise the right click event on the LeftClickButton itself, whenever the left button is clicked. I have tried the following with no luck:
private void LeftButtonClick_Click(object sender, System.Windows.RoutedEventArgs e)
{
MouseButtonEventArgs mouseButtonEventArgs = new MouseButtonEventArgs(mouseDevice, 0, MouseButton.Right);
mouseButtonEventArgs.RoutedEvent = Mouse.MouseUpEvent;
mouseButtonEventArgs.Source = this;
this.RaiseEvent(mouseButtonEventArgs);
{
When left clicked the button calls the function, but nothing ever happens. Right clicks cause a context menu to appear and all I need is for left clicks to make the context menu to appear. I think all I really need to know is how to raise the MouseRightButtonUpEvent within this button. This has eluded me for a good two days. Quite baffling for all the methods I have tried, which I will not list here. The above code looked the most promising.
Defined in a resource dictionary:
<ContextMenu
x:Key="MyContextMenu"
Placement="Bottom"
Style={StaticResource ContextMenuStyle1}">
<MenuItem
Command="{Binding Path=SetStuffCommand}"
Header="Set Stuff"
Template="{StaticRsource MenuItemControlTemplate1}"/>
</ContextMenu>
<Style x:Key="MyContextMenuButton" TargetType="{x:Type view:LeftClickButton)">
<Setter Property="ContextMenuService.Placement" Value="Bottom"/>
<Setter Property="ContextMenu" Value"{StaticResource MyContextMenu}"/>
</Style>
Upvotes: 0
Views: 1737
Reputation: 41
Alright, here is the crucial code for what I ended up doing. Mind you, I did not ever really figure out how to get the same response, through code, out of a button as if I had right clicked it, but I did get my context menu to launch and in the right place. I actually changed the class to one derived from TextBlock as opposed to Button, and used the PreviewMouseLeftButtonUp to take the place of the Click method:
public class LeftClickTextBlock : TextBlock
{
public LeftClickTextBlock()
{
this.PreviewMouseLeftButtonUp += this.LeftClickTextBlock_PreviewMouseLeftButtonUp;
}
private void LeftClickTextBlock_PreviewMouseLeftButtonUp(object sender, RoutedEventArgs e)
{
LeftClickTextBlock lcb = sender as LeftClickTextBlock;
ContextMenu contextMenu = lcb.ContextMenu;
contextMenu.PlacementTarget = lcb;
contextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
contextMenu.IsOpen = true;
}
}
And then, of course, this needed slightly adjusted styles:
<Style x:Key="MyContextMenuTextBlock" TargetType="{x:Type view:LeftClickTextBlock)">
<Setter Property="ContextMenuService.Placement" Value="Bottom"/>
<Setter Property="ContextMenu" Value"{StaticResource MyContextMenu}"/>
</Style>
This was ultimately placed within a data template that allows me to control not only the type of context menu that appears when the an individual data grid element is clicked, but also what view model level object the context menu's menu items are bound to. I won't post all the code here, but I'm thinking about posting it somewhere, because it really gives full view model control over every cell and binding in the data grid.
Again, defined in a ResourceDictionary or in the App.xaml:
<DataTemplate DataType={x:Type viewModel:DataItem}>
<view:LeftClickTextBlock
Background="Transparent"
Style="{StaticResource MyContextMenuTextBlock}"
Text="{Binding Path=DataString, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
Upvotes: 1