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

個人的には、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 に答える
1

イベントハンドラー(ラベルのマウスイベントを登録)を追加していないようです:

xrLabel260.MouseEnter += xrLabel260_MouseEnter;

これを実行する最も論理的な場所は、フォームのロード ルーチンです。

編集:WPFの場合、XAMLで次のようなものを使用できます(質問にはMouseEventArgsの代わりにEventArgsがありましたが、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 に答える