【问题标题】:Changing the fore color of a label when hovering over the label将鼠标悬停在标签上时更改标签的前景色
【发布时间】:2013-04-28 01:54:21
【问题描述】:

当我将鼠标悬停在标签上时,我正在尝试更改标签文本的颜色。我尝试将命令放在 previewmousemove 事件中,但这不起作用。

    private void hand_PreviewMouseMove(object sender, PreviewMouseEventArgs e)
        {
            Cursor.Current = Cursors.Hand;
            xrLabel260.BackColor = Color.CornflowerBlue;
        }

这不起作用后,我尝试使用 mouseenter/mouseleave 事件来更改颜色。

    private void xrLabel260_MouseEnter(object sender, EventArgs e)
    {

        xrLabel260.ForeColor = Color.CornflowerBlue;

    }

    private void xrLabel260_MouseLeave(object sender, EventArgs e)
    {
        xrLabel260.ForeColor = Color.Black;

    }

这也不起作用。我怎样才能更改我的代码以使其正常工作?预先感谢您的帮助。

【问题讨论】:

  • previewmousemove 正在触发,但似乎没有办法在 devexpress 设计器中使用 mouseenter 和 mouseleave 事件。

标签: c# devexpress mousehover onmousemove


【解决方案1】:

我个人会在你的 xaml 中做这样的事情: 编辑:我已经修改了它以显示它如何适合主窗口。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Label Content="My Label">
        <Label.Style>
            <Style TargetType="Label">
                <Setter Property="Foreground" Value="Black"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="Blue"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
</Grid>

【讨论】:

  • 我是这个程序的新手。这会放在 MainWindow.xaml 中吗?
  • 看看我的编辑。如果这仍然不清楚,请将您的代码发送给我,我会帮助您。我的电子邮件在我的个人资料中。
【解决方案2】:

您似乎没有为它添加事件处理程序(为标签注册鼠标事件):

xrLabel260.MouseEnter += xrLabel260_MouseEnter;

最合乎逻辑的地方是在表单的加载例程中。

编辑:对于 WPF,您可以在 XAML 中使用类似的东西(问题有 EventArgs 而不是 MouseEventArgs,我认为它是用于 WinForms):

<Label x:Name="xrLabel260" Content="Label" MouseEnter="xrLabel260_MouseEnter"/>

...然后在代码隐藏中:

    private void xrLabel260_MouseEnter(object sender, MouseEventArgs e)
    {
        xrLabel260.Foreground =  Brushes.BlanchedAlmond;
    }

【讨论】:

  • 感谢您的回复。我使用的是 WPF 而不是 winforms。我会把这个放在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 2023-01-21
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
相关资源
最近更新 更多