单击IconButton后如何打开ContextMenu?

时间:2015-09-24 13:32:51

标签: c# wpf xaml mvvm

左键单击IconButton后我需要打开ContextMenu ... 我的按钮有这个代码:

<Controls:IconButton Visibility="{Binding IsFunctionalityVisible, Converter={StaticResource BooleanToVisibilityConverter}}"  Margin="0,0,0,0" Content="&#xf039;" ToolTip="Více možností">
              <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                  <cal:ActionMessage MethodName="OpenContextMenu">
                  </cal:ActionMessage>
                </i:EventTrigger>
              </i:Interaction.Triggers>
</Controls:IconButton>

问题是,我不能像这样使用ActionMessage,因为我不知道如何创建函数&#34; OpenContextMenu&#34;在ViewModel中。

我可以使用弹出窗口而不是ContextMenu(以某种方式将弹出窗口添加到我的IconButton样式中吗?)?

编辑: 这是我对IconButton的风格:

<Style x:Key="IconButtonStyle" TargetType="{x:Type Button}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="{StaticResource ButtonForegroundColor}" />
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
      <Border  BorderThickness="0" Background="{TemplateBinding Background}" CornerRadius="0" SnapsToDevicePixels="true" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <ContentControl x:Name="ContentControl" Content="{TemplateBinding Content}" FontFamily="{StaticResource IconFont}" Margin="{TemplateBinding Padding}"  FontSize="{TemplateBinding FontSize}" Foreground="{TemplateBinding Foreground}"  HorizontalAlignment="Center" VerticalAlignment="Center"/>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="Button.IsDefaulted" Value="true">
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
          <Setter Property="Foreground" TargetName="ContentControl" Value="{StaticResource ButtonForegroundActiveColor}"/>
        </Trigger>
        <Trigger Property="IsPressed" Value="true">
          <Setter Property="Foreground" TargetName="ContentControl" Value="{StaticResource ButtonForegroundActiveColor}"/>
        </Trigger>
      </ControlTemplate.Triggers>  
    </ControlTemplate>
  </Setter.Value>
</Setter>

编辑2:在上一张图片上点击蓝色的IconButton之后我想要这样的ContextMenu(不是在点击后)

I want ContextMenu like this

After clicking this IconButton

1 个答案:

答案 0 :(得分:0)

您可以使用Image的MouseDown事件执行此操作:

<Controls:IconButton ... MouseDown="Image_MouseDown">
<Controls:IconButton.ContextMenu>
    <ContextMenu>
        <MenuItem .../>
        <MenuItem .../>
    </ContextMenu>
</Controls:IconButton.ContextMenu>
</Controls:IconButton>

然后在代码后面的EventHandler中显示ContextMenu:

private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
       e.Handled = true;
       Image image = sender as Image;
       ContextMenu contextMenu = image.ContextMenu;
       contextMenu.PlacementTarget = image;
       contextMenu.IsOpen = true;
    }
}