C# 单击事件右键单击事件数据绑定模板
我正在与WPF MVVM合作 我制作了以下数据模板:C# 单击事件右键单击事件数据绑定模板,c#,wpf,mvvm,data-binding,datatemplate,C#,Wpf,Mvvm,Data Binding,Datatemplate,我正在与WPF MVVM合作 我制作了以下数据模板: 如何将事件从激活上下文菜单的右键单击绑定到左键单击?在边框元素上创建一个新的左键处理程序: 它所做的,基本上是将左键单击映射为右键单击。什么是Source=Win@Chris?@Darknesstiller;它是边界元素的名称。就我而言,赢。您可以将其设置为Source=sender <DataTemplate DataType="{x:Type r:CustomControl}"> <Border x:Na
如何将事件从激活上下文菜单的右键单击绑定到左键单击?在边框元素上创建一个新的左键处理程序:
它所做的,基本上是将左键单击映射为右键单击。什么是Source=Win@Chris?@Darknesstiller;它是边界元素的名称。就我而言,赢。您可以将其设置为Source=sender
<DataTemplate DataType="{x:Type r:CustomControl}">
<Border x:Name="bord" BorderThickness="0" Width="150" Height="150" Margin="0"
BorderBrush="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Background}"
Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type con:Control}, Mode=FindAncestor}, Path=TileColorPair[0]}"
ContextMenu="{StaticResource CMenu}">
<Button Width="{Binding Size}" Height="{Binding Size}">
<Button.Template>
<ControlTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image x:Name="img" Grid.Row="0" Source="{Binding ImageUrl}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Label Grid.Row="1" Content="{Binding Text}" FontFamily="{DynamicResource DefaultFont}" FontSize="{DynamicResource {x:Static SystemFonts.CaptionFontSizeKey}}"
Foreground="White" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
</Border>
<ContextMenu x:Key="CMenu">
<MenuItem Command="{Binding UpdateCommand}" Header="Update">
<MenuItem.Icon>
<Image Width="45" Height="45" Source="/Assembly;component/Resources/edit.png"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="{Binding SelectCommand}" Header="Select">
<MenuItem.Icon>
<Image Width="45" Height="45" Source="/Assembly;component/Resources/search.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
<Border x:Name="Win"
Width="40"
Height="40"
Background="Purple"
MouseLeftButtonUp="UIElement_OnMouseLeftButtonUp">
private void UIElement_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
var mouseDownEvent =
new MouseButtonEventArgs(Mouse.PrimaryDevice,
Environment.TickCount,
MouseButton.Right)
{
RoutedEvent = Mouse.MouseUpEvent,
Source = Win,
};
InputManager.Current.ProcessInput(mouseDownEvent);
}