如何在 WPF 中更改鼠标悬停事件上的边框画笔



当鼠标悬停在按钮上时,我正在尝试更改BorderBrush的颜色。我已经为按钮创建了一个新的控件模板,但是在设置触发器时,Visual Studio 告诉我我必须使用EventTrigger但是当我使用它时,没有MouseOver事件,只有一个MouseEnter事件。应用此按钮并运行解决方案时,边框画笔不会更改。有什么建议吗?

编辑:

代码我遇到问题:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.Resources>
        <ControlTemplate x:Key="LogInButton">
            <Grid Width="AUTO" Height="AUTO">
                <Border x:Name="ButtonBorder"
                                    BorderBrush="#B7B7B7"
                                    BorderThickness="2"
                                    Background="Transparent"
                                    Cursor="Hand">
                    <Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>

                    <Border.Triggers>
                        <EventTrigger RoutedEvent="MouseMove">
                            <EventTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetName="ButtonBorder"
                                                            Storyboard.TargetProperty="BorderBrush"
                                                            To="White"
                                                            Duration="0"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.EnterActions>
                        </EventTrigger>
                    </Border.Triggers>

                </Border>
            </Grid>
        </ControlTemplate>
    </Grid.Resources>
    <Button Template="{StaticResource LogInButton}" Tag="Images/Login.png" Height="50" Width="50" Background="Transparent">
    </Button>
</Grid>

您可以只向<ControlTemplate.Triggers>添加Trigger,而不是向<Border.Triggers>添加EventTrigger

<ControlTemplate x:Key="LogInButton">
    <Grid Width="AUTO" Height="AUTO">
        <Border x:Name="ButtonBorder"
                BorderBrush="#B7B7B7"
                BorderThickness="2"
                Background="Transparent"
                Cursor="Hand">
            <Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"/>
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter TargetName="ButtonBorder" Property="BorderBrush" Value="White" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

最新更新