使用触发器将按钮 IsEnabled 设置为鼠标悬停时为 true

发布于 2024-10-08 18:38:24 字数 669 浏览 7 评论 0原文

我正在尝试设置一个按钮,因此只有当鼠标经过它时 按钮会将其视觉属性更改为启用按钮。

这是我的代码:

    <Button Foreground="Black" Content="OK" Margin="186,100,170,172" >
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsEnabled" Value="True" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

提前感谢您的帮助。

I am trying to set a button so only when a mouse is going over it the
button will change his visual properties to an enabled button.

This is my code:

    <Button Foreground="Black" Content="OK" Margin="186,100,170,172" >
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsEnabled" Value="True" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

Thanks in advance for your help.

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

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

发布评论

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

评论(2

剧终人散尽 2024-10-15 18:38:24

您可以将 Button 封装在 Border 中以获得您想要的东西

<Border Name="buttonBorder" Margin="186,100,170,172"
        Background="#01000000" BorderBrush="Transparent" BorderThickness="0">
    <Button Foreground="Black" Content="OK">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=buttonBorder, Path=IsMouseOver}" Value="True">
                        <Setter Property="IsEnabled" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
</Border>

You could encapsulate your Button in a Border to get what you're after

<Border Name="buttonBorder" Margin="186,100,170,172"
        Background="#01000000" BorderBrush="Transparent" BorderThickness="0">
    <Button Foreground="Black" Content="OK">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="IsEnabled" Value="False" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=buttonBorder, Path=IsMouseOver}" Value="True">
                        <Setter Property="IsEnabled" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
</Border>
云雾 2024-10-15 18:38:24

我认为您不可能在这里执行此操作,因为当按钮被禁用时,IsMouseOver 为 false(而且,它不会生成 MouseMove 或其他事件)。

为了实现你想要的(顺便说一句,为什么当你将鼠标放在一个禁用的按钮上时,你会想要启用它?这太不自然了......如果你想用键盘导航到按钮怎么样?这是不可能的如果它被禁用),您应该重新设置按钮的样式,并使其在鼠标未位于其上方时看起来处于禁用状态,而当鼠标位于其上方时看起来处于启用状态 - 但这需要一些工作)。

I don't think it's possible what you want to do here, because while the button is disabled, IsMouseOver is false (also, it doesn't generate MouseMove or other events).

To achieve what you want (btw, why would you want a disabled button to become enabled when you put the mouse over it? It's so unnatural... how about if you want to navigate with the keyboard to the button? it will be impossible if it is disabled), you should restyle the button and make it look as in its disabled state when the mouse is not over it and as in its enabled state when the mouse is over it - but this will require some work).

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