如何编写 WPF 触发器来更改文本块悬停上的光标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16585016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:48:07  来源:igfitidea点击:

How do I write WPF Trigger to change cursor on textblock hover

wpfxaml

提问by John

I have a textblock that currently has a trigger that sets the foreground colour when the mouse enters and back to the default when it leaves. The problem I have is that I would also like the mouse pointer to change I currently have the following

我有一个当前有一个触发器的文本块,它在鼠标进入时设置前景色并在鼠标离开时返回默认值。我的问题是我也希望鼠标指针改变我目前有以下

    <Style TargetType="TextBlock" x:Key="FlatStyleButton">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Background" Value="#FF333333" />
        <Style.Triggers>
            <EventTrigger RoutedEvent="UIElement.MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Foreground.Color" To="CornflowerBlue" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.MouseLeave">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Foreground.Color" To="White" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>

I have tried adding <Setter Property="Cursor" Value="Hand"></Setter>to various places but it never seems to work

我曾尝试添加<Setter Property="Cursor" Value="Hand"></Setter>到各个地方,但似乎从未奏效

回答by John

Sorry guys Proper school boy error on my part im afraid, what I was trying would have worked but I was modifiying in the wrong resource file. So if anyone else is intrested the answer was:

对不起,伙计们,我担心是正确的男生错误,我尝试的方法会奏效,但我正在修改错误的资源文件。因此,如果其他人感兴趣,答案是:

<Style TargetType="TextBlock" x:Key="FlatStyleButton">
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Background" Value="#FF333333" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Cursor" Value="Hand" />
            </Trigger>
            <EventTrigger RoutedEvent="UIElement.MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Foreground.Color" To="CornflowerBlue" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="UIElement.MouseLeave">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Duration="0:0:0.3" Storyboard.TargetProperty="Foreground.Color" To="White" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>