首页 > 解决方案 > 在 Windows Presentation Foundation WPF C# 中禁用自动鼠标悬停

问题描述

我创建了一个 Windows Presentation Foundation WPF 应用程序。

问题是当我将鼠标悬停在按钮上时会产生蓝色闪烁的自动 MouseHover。

我检查了所有组件,堆栈面板,网格,按钮......等,在我的选项中没有 MouseHover 动作,它是某种默认悬停,我无法禁用它。我可以以某种方式禁用或更改它吗?

在此处输入图像描述

<Window x:Class="EnovaLauncher.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:EnovaLauncher"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Loaded="Window_Loaded"
        Title="Enova launcher" Height="586" Width="800" Background="#FF19680C">
    <Grid VerticalAlignment="Center" Margin="0,-60,0,0" Height="483">
        <StackPanel Margin="0,0,0,-59">
            <StackPanel.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="Margin" Value="4" />
                </Style>
                <Style TargetType="TextBox">
                    <Setter Property="Margin" Value="4" />
                </Style>
                <Style TargetType="ComboBox">
                    <Setter Property="Margin" Value="4" />
                </Style>
                <Style TargetType="Button">
                    <Setter Property="Margin" Value="4" />
                </Style>
            </StackPanel.Resources>
            <TextBlock Foreground="White">Ostatnio uruchamiane</TextBlock>
            <DockPanel>
                <Button DockPanel.Dock="Right" x:Name="btn_Delete" Click="btn_Delete_Click">X</Button>
                <ComboBox Name="cb_Recent" SelectionChanged="cb_Recent_SelectionChanged"></ComboBox>
            </DockPanel>

            <TextBlock Foreground="White">Parametry</TextBlock>
            <TextBlock Foreground="White">Nazwa</TextBlock>
            <TextBox x:Name="tb_Name"></TextBox>
            <Grid>
                <StackPanel>
                    <StackPanel>
                        <TextBlock Foreground="White">Wersja</TextBlock>
                        <ComboBox Name="cb_Version"></ComboBox>
                    </StackPanel>
                    <StackPanel Grid.Column="1">
                        <TextBlock Foreground="White">DBextensions</TextBlock>
                        <CheckBox x:Name="checkbox"  Foreground="White" Content="Dodaj" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,5,0,5"/>
                    </StackPanel>
                    <StackPanel Grid.Column="2">
                        <TextBlock Foreground="White">Operator</TextBlock>
                        <TextBox x:Name="tb_Op"></TextBox>
                    </StackPanel>
                    <StackPanel Grid.Column="3">
                        <TextBlock Foreground="White">DbConfig</TextBlock>
                        <ComboBox Name="cb_Db" SelectionChanged="cb_Db_SelectionChanged">
                            <ComboBox.Background>
                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                    <GradientStop Color="#FFF0F0F0" Offset="0"/>
                                    <GradientStop Color="#FFE5E5E5" Offset="1"/>
                                </LinearGradientBrush>
                            </ComboBox.Background>
                        </ComboBox>
                    </StackPanel>
                    <StackPanel Grid.Column="4">
                        <TextBlock Foreground="White">ExtPath</TextBlock>
                        <ComboBox Name="cb_Extensions" SelectionChanged="cb_Extensions_SelectionChanged">
                            <ComboBox.Background>
                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                    <GradientStop Color="#FFF0F0F0" Offset="0"/>
                                    <GradientStop Color="#FFE5E5E5" Offset="1"/>
                                </LinearGradientBrush>
                            </ComboBox.Background>
                        </ComboBox>
                    </StackPanel>
                </StackPanel>
            </Grid>
            <Button x:Name="btn_Load"  Height="40" Click="btn_LoadFromShortcut" Content="Załaduj skrót" Background="#FF498D11" FontSize="14" Foreground="White"/>
            <Button x:Name="btn_Shortcut" Height="40" Click="btn_Shortcut_Click" Content="Utwórz skrót" Background="#FF498D11" Foreground="White"  FontSize="14"/>
            <Button x:Name="btn_Launch" Height="40" Click="btn_Launch_Click" Content="Uruchom" Background="#FF498D11" Foreground="White"  FontSize="14"/>

        </StackPanel>
    </Grid>
</Window>

标签: c#windows-forms-designer

解决方案


您必须为您的 定义Stylea ,然后在其中定义某些 的行为。您正在寻找的称为. 这里有一个小例子,颜色设置为 Aqua。TemplateButtonTriggersTriggerIsMouseOverBackground

<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border
                    Width="{TemplateBinding Width}"
                    Height="{TemplateBinding Height}"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}"
                    CornerRadius="0"
                    SnapsToDevicePixels="True">
                    <ContentPresenter 
                        Margin="{TemplateBinding Padding}"/>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <!-- Define your MouseOver behaviour here! E.g.:-->
                        <Setter Property="Background" Value="Aqua" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

推荐阅读