C# 闪烁按钮(非常基本)

C# 闪烁按钮(非常基本),c#,wpf,xaml,C#,Wpf,Xaml,所以我创建了一个非常简单的程序,因为我刚刚开始学习WPF 事实上,它非常简单,我可以在这里写下所有内容: <Window x:Class="TestWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Heigh

所以我创建了一个非常简单的程序,因为我刚刚开始学习WPF

事实上,它非常简单,我可以在这里写下所有内容:

<Window x:Class="TestWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="650" Width="825">
<Grid>
    <StackPanel Width="125" Background="AliceBlue" Margin="0,10,200,10">
        <Button Margin="5,5,5,5" Height="50" Width="100" Content="Tester" Background="Coral">

        </Button>    
    </StackPanel>

</Grid>
但是这个程序有一个问题。按钮闪烁

当我加载程序时,按钮是预期的珊瑚色。如果我把鼠标移到它上面,它会回到原来的颜色。我猜颜色来自于控制它的控件?如你所见,我只为按钮指定了一种颜色

当我点击鼠标左键时,问题出现了。当我这样做时,按钮会在大约一秒钟的时间内从一种颜色的珊瑚色过渡到另一种颜色的爱丽丝蓝。它一次又一次地来回移动。如果我在这种状态下将鼠标移到它上面,它会像往常一样返回鼠标移到颜色上,但当我将鼠标从它身上移开时,它又开始闪烁

需要明确的是:这与鼠标上方的颜色变化无关。我同意。我点击按钮后,所述按钮在两种颜色之间反复转换。颜色是珊瑚色和鼠标悬停的颜色,我当然没有具体说明

我在这里不知所措。我没叫它这么做,是吗?我没有单击属性中的任何内容,也没有编写任何代码。XAML就是我所做的一切

按钮到底为什么闪烁

编辑这里有一些显示了整个事情。所有代码都没有。XAML,app.XAML.cs,一切


编辑2另一个包含所有图片的图像。据我所知,我没有更改任何样式。

这是因为windows中的默认样式

windows 7上的普通按钮从双色调灰色转换为双色调蓝色,由默认控件模板触发

您可以编辑控件模板并在按钮上设置样式

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApplication1.MainWindow"
    Title="MainWindow" Height="650" Width="825">
    <Window.Resources>
        <Style x:Key="ButtonFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#F3F3F3" Offset="0"/>
            <GradientStop Color="#EBEBEB" Offset="0.5"/>
            <GradientStop Color="#DDDDDD" Offset="0.5"/>
            <GradientStop Color="#CDCDCD" Offset="1"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Width="125" Background="AliceBlue" Margin="0,10,200,10">
            <Button Margin="5,5,5,5" Height="50" Width="100" Content="Tester" Background="Coral" Style="{DynamicResource ButtonStyle1}"/>
        </StackPanel>

    </Grid>
</Window>
您现在可以看到其他颜色来自何处


从中删除属性RenderMouseOver={TemplateBinding IsMouseOver}RenderPressed={TemplateBinding IsPressed}RenderDefaulted={TemplateBinding IsDefaulted}这是因为windows中的默认样式

windows 7上的普通按钮从双色调灰色转换为双色调蓝色,由默认控件模板触发

您可以编辑控件模板并在按钮上设置样式

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApplication1.MainWindow"
    Title="MainWindow" Height="650" Width="825">
    <Window.Resources>
        <Style x:Key="ButtonFocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#F3F3F3" Offset="0"/>
            <GradientStop Color="#EBEBEB" Offset="0.5"/>
            <GradientStop Color="#DDDDDD" Offset="0.5"/>
            <GradientStop Color="#CDCDCD" Offset="1"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
            <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Themes:ButtonChrome>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ADADAD"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel Width="125" Background="AliceBlue" Margin="0,10,200,10">
            <Button Margin="5,5,5,5" Height="50" Width="100" Content="Tester" Background="Coral" Style="{DynamicResource ButtonStyle1}"/>
        </StackPanel>

    </Grid>
</Window>
您现在可以看到其他颜色来自何处


从中删除属性RenderMouseOver={TemplateBinding IsMouseOver}RenderPressed={TemplateBinding IsPressed}RenderDefaulted={TemplateBinding IsDefaulted}您遇到的闪烁可能来自按钮的默认模板。您可以覆盖它,使按钮看起来像一个没有任何效果的普通矩形:

<Button Margin="5,5,5,5" Height="50" Width="100" Content="Tester" Background="Coral">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Margin}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

您正在经历的闪烁可能来自按钮的默认模板。您可以覆盖它,使按钮看起来像一个没有任何效果的普通矩形:

<Button Margin="5,5,5,5" Height="50" Width="100" Content="Tester" Background="Coral">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Margin}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
            </Border>
        </ControlTemplate>
    </Button.Template>
</Button>

无法用您提供的代码重现您描述的内容。@R.Rusev他说的是背景颜色更改上的普通WPF按钮鼠标。无论如何,在Win7中,我完全明白他在做什么。Flash根本不是我用来描述它的词,但他说效果是鼠标在按钮上时颜色不同。@EdPlunkett我不是这么说的。我的意思是,当我点击按钮,然后将鼠标移离它时,它进入一个循环,从珊瑚色到默认颜色,再回到珊瑚色,再回到默认颜色,依此类推无限期。@TheFaithfullLeaner哦,好的。很抱歉我没有尝试单击从您的XAML复制的测试按钮。但现在我点击它,我看到的正是你们所看到的:它在蓝色和珊瑚色之间循环,只要它保持键盘焦点。当我弹出选项卡或单击另一个控件时,循环停止。事实上,如果我去掉背景属性,我会再次看到相同的,但它会在默认的惰性灰色渐变和活动的蓝色渐变之间循环,这是一个更微妙的区别,但仍然很明显。您是否尝试按照建议定义自定义模板?无法用您提供的代码重现您描述的内容。@R.Rusev他说的是普通WPF按钮鼠标背景颜色的变化。无论如何,在Win7中,我完全明白他在做什么。Flash根本不是我用来描述它的词,但他说效果是鼠标在按钮上时颜色不同。@EdPlunkett我不是这么说的。我的意思是,当我点击按钮,然后将鼠标移离它时,它进入一个循环,从珊瑚色到默认颜色,再回到珊瑚色,再回到默认颜色,依此类推无限期。@TheFaithfullLeaner哦,好的。很抱歉我没有尝试单击从您的XAML复制的测试按钮。但现在我点击它,我看到的正是你们所看到的:它在蓝色和珊瑚色之间循环,只要它保持键盘焦点。当我脱身时
或者单击另一个控件,循环停止。事实上,如果我去掉Background属性,我会再次看到相同的属性,但它会在默认的惰性灰色渐变和活动蓝色渐变之间循环,这是一个更细微的区别,但仍然很明显。您是否尝试按照建议定义自定义模板?但自定义模板没有在XAML中指定,那么windows默认模板是如何修复的呢?@foobar,总有一个模板。可以在mm8的答案中指定自定义选项,也可以使用样式中的选项。在我的示例中,虽然它是默认值的副本,但它仍然是自定义的。你不能修复windows默认模板/样式,但你可以编辑一个副本,并用它做任何你喜欢的事情。我是一个WPF新手,所以我不确定把你的XAML放在哪里。我尝试了XAML的顶部,在“窗口”标签之外。当我这样做时,我得到了5个错误,其中第一个是找不到“类型”样式。验证您没有丢失程序集引用,并且所有引用的程序集都已生成。“@jamesbarras然后我尝试在窗口标记内,网格标记上方。如果我这样做,我会得到6个错误,其中第一个错误是:'thetype'主题:ButtonChrome'未找到。验证是否缺少程序集引用,以及是否已删除所有引用的程序集bu@TheFaithfulLearner已更新以包含缺少的位。如果右键单击->编辑模板->编辑副本,VisualStudio应该可以为您完成此操作。。。按按钮。我认为这取决于你的VS版本谢谢你的持续帮助,现在看起来确实更好了,除了一个错误:“没有找到类型主题:ButtonChrome。但是自定义模板没有在XAML中指定,那么windows默认模板如何修复?@foobar,总有一个模板。可以在mm8的答案中指定自定义选项,也可以使用样式中的选项。在我的示例中,虽然它是默认值的副本,但它仍然是自定义的。你不能修复windows默认模板/样式,但你可以编辑一个副本,并用它做任何你喜欢的事情。我是一个WPF新手,所以我不确定把你的XAML放在哪里。我尝试了XAML的顶部,在“窗口”标签之外。当我这样做时,我得到了5个错误,其中第一个是找不到“类型”样式。验证您没有丢失程序集引用,并且所有引用的程序集都已生成。“@jamesbarras然后我尝试在窗口标记内,网格标记上方。如果我这样做,我会得到6个错误,其中第一个错误是:'thetype'主题:ButtonChrome'未找到。验证是否缺少程序集引用,以及是否已删除所有引用的程序集bu@TheFaithfulLearner已更新以包含缺少的位。如果右键单击->编辑模板->编辑副本,VisualStudio应该可以为您完成此操作。。。按按钮。我认为这取决于你的VS版本谢谢你的持续帮助,现在看起来确实更好了,除了一个错误:“没有找到类型主题:ButtonChrome。”。