左键单击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,因为我不知道如何在ViewModel中创建函数“OpenContextMenu”。

我可以使用例如Pop-up代替ContextMenu(以某种方式将Pop-up添加到我的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:我想要ContextMenu像这样在单击最后一张图片上的蓝色IconButton之后(而不是右键单击之后)

c# - 单击IconButton后如何打开ContextMenu?-LMLPHP

c# - 单击IconButton后如何打开ContextMenu?-LMLPHP

最佳答案

您可以通过使用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;
    }
}

关于c# - 单击IconButton后如何打开ContextMenu?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32762744/

10-15 06:47