2014-12-29 43 views
0

我的工作與WPF MVVMClick事件右擊事件數據綁定模板

我做了如下DataTemplate

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

我在Border相關的ContextMenu。 每個菜單都有一個特定的Command

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

如何綁定從右鍵激活的ContextMenu的左鍵以及事件?

回答

0

創建Border元素上一個新的左按鈕處理程序:

<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); 
} 

它做什麼,它基本上映射左點擊進入單擊鼠標右鍵。

+0

什麼是Source = Win' @Chris? – Darknesstiller

+0

@Darknesstiller;它是'Border'元素的名稱。就我而言,「贏」。您可以將其設置爲「Source = sender」 –