C# 单击图标按钮后如何打开上下文菜单?
我需要打开上下文菜单后,左键点击图标按钮。。。 我的按钮有以下代码:C# 单击图标按钮后如何打开上下文菜单?,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,我需要打开上下文菜单后,左键点击图标按钮。。。 我的按钮有以下代码: <Controls:IconButton Visibility="{Binding IsFunctionalityVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,0,0,0" Content="" ToolTip="Více možností"> <i
<Controls:IconButton Visibility="{Binding IsFunctionalityVisible, Converter={StaticResource BooleanToVisibilityConverter}}" Margin="0,0,0,0" Content="" 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”
例如,我可以使用弹出窗口而不是上下文菜单(以某种方式将弹出窗口添加到我的图标按钮样式中?)
编辑:
这是我对图标按钮的风格:
<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:在点击最后一张图片上的蓝色图标按钮后,我想要这样的上下文菜单(不是在右键点击后)
您可以使用图像的MouseDown事件来执行此操作,如下所示:
<Controls:IconButton ... MouseDown="Image_MouseDown">
<Controls:IconButton.ContextMenu>
<ContextMenu>
<MenuItem .../>
<MenuItem .../>
</ContextMenu>
</Controls:IconButton.ContextMenu>
</Controls:IconButton>
您可以使用图像的MouseDown事件来执行此操作,如下所示:
<Controls:IconButton ... MouseDown="Image_MouseDown">
<Controls:IconButton.ContextMenu>
<ContextMenu>
<MenuItem .../>
<MenuItem .../>
</ContextMenu>
</Controls:IconButton.ContextMenu>
</Controls:IconButton>
您确定要关联菜单吗?听起来你有一个常规菜单,上面有一个最重要的项目。没有?这是我在工作中告诉我的。他们想要一个上下文菜单。我是xaml新手,如何在那里添加常规菜单?我会添加上下文菜单的截图。你确定要上下文菜单吗?听起来你有一个常规菜单,上面有一个最重要的项目。没有?这是我在工作中告诉我的。他们想要一个上下文菜单。我是xaml新手,如何在那里添加常规菜单?我将添加上下文菜单的截图,我的意思是。我必须用更复杂的方式来做这件事。。。我必须使用DependencyProperty等等,因为模板,我必须使用更复杂的方法来实现这一点。。。由于模板的原因,我不得不使用DependencyProperty等