仅使用 XAML 在按钮上 IsMouseOver 时调出 ContextMenu

发布于 2024-12-10 04:05:39 字数 1001 浏览 0 评论 0原文

我正在尝试使用 XAML(仅,无代码隐藏)来调出按钮的 ContextMenu。

我的按钮在这里

<Button x:Name="btn" Style="{StaticResource mybutton}" >
<Button.ContextMenu>
    <ContextMenu>
        <TextBlock Text="Information"/>
    </ContextMenu>
</Button.ContextMenu>
</Button>

按钮的样式在这里

<Style TargetType="{x:Type Button}" x:Key="mybutton">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="ContextMenu.IsOpen" Value="True"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

我的 google-fu 令我失望,因为这似乎是一个简单的解决方案。我真的更愿意避免使用代码隐藏(MouseEnter/MouseLeave 事件)。

先感谢您。

I am trying to use XAML (only, no codebehind) to bring up the ContextMenu of a button.

I have this my button here

<Button x:Name="btn" Style="{StaticResource mybutton}" >
<Button.ContextMenu>
    <ContextMenu>
        <TextBlock Text="Information"/>
    </ContextMenu>
</Button.ContextMenu>
</Button>

The Style for the button here

<Style TargetType="{x:Type Button}" x:Key="mybutton">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="ContextMenu.IsOpen" Value="True"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

My google-fu is failing me for what seems like an easy solution. I really would prefer to avoid using codebehind (MouseEnter/MouseLeave events).

Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

口干舌燥 2024-12-17 04:05:39

尝试通过在“TargetName”属性中提供名称来将“Setter”应用于 ControlTemplate 中的 ContextMenu。例如:

<Button Width="100" Height="100" x:Name="btn">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border CornerRadius="2" BorderThickness="3" BorderBrush="DarkGray" x:Name="border">
                                <Border.ContextMenu>
                                    <ContextMenu x:Name="cmenu">
                                        <TextBlock>Information</TextBlock>
                                    </ContextMenu>
                                </Border.ContextMenu>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="ContextMenu.IsOpen" Value="True" TargetName="cmenu"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>

Try to apply "Setter" for a ContextMenu within the ControlTemplate, by providing it's name in the "TargetName" property. For example:

<Button Width="100" Height="100" x:Name="btn">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Border CornerRadius="2" BorderThickness="3" BorderBrush="DarkGray" x:Name="border">
                                <Border.ContextMenu>
                                    <ContextMenu x:Name="cmenu">
                                        <TextBlock>Information</TextBlock>
                                    </ContextMenu>
                                </Border.ContextMenu>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="ContextMenu.IsOpen" Value="True" TargetName="cmenu"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>
笨死的猪 2024-12-17 04:05:39

我猜这就是你想要的 -
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/adafe007-9637-4f28-8366-8f14ead2bd75

All you need to do is capture the mouse event that you want to trigger the context menu.

This is what you want i guess -
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/adafe007-9637-4f28-8366-8f14ead2bd75

All you need to do is capture the mouse event that you want to trigger the context menu.
锦上情书 2024-12-17 04:05:39

最好在代码隐藏中使用 mouse_up 事件。使用 MouseOver 时,用户会感到被愚弄,因为当鼠标移向某个条目时,上下文菜单消失了......
在 VB 中,代码如下所示:

Private Sub image_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles image.MouseUp
    anyControl.ContextMenu.IsOpen = Not OrteListBox.ContextMenu.IsOpen
End Sub

anyCountrol 代表发送者或保存 ContextMenu 的任何其他控件

It is best to use a mouse_up event in codebehind. With MouseOver the user feels fooled as the context menu is gone when the mouse moves towards an entry...
In VB the code looks like:

Private Sub image_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles image.MouseUp
    anyControl.ContextMenu.IsOpen = Not OrteListBox.ContextMenu.IsOpen
End Sub

anyCountrol stands for the sender or any other control that holds the ContextMenu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文