0

当我将鼠标悬停在标签上时,我正在尝试更改标签文本的颜色。我尝试将命令放在 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;

    }

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

4

2 回答 2

1

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

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;
    }
于 2013-05-03T15:51:11.390 回答
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>

于 2013-05-03T15:45:14.070 回答