按钮在单击按钮时会产生意想不到的效果

时间:2012-08-07 08:00:47

标签: c# wpf silverlight xaml wpf-controls

伙计们,我对WPF广告很新,开始用Sam的电子书学习它。我刚试过书中给出的相同代码如下

<Window x:Class="wpfTest.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">

    <Button Background="Black">
        <Button.Content>
            <Ellipse Width="103" Height="107" Fill="Yellow" />
        </Button.Content>
    </Button>
</Window>

可惜的是,当我一次点击按钮时,它开始慢慢闪烁,给出指定的黑色背景以及白色/蓝色背景,它永远不会停止。我不知道为什么默认行为是这样的,因为我没有提到任何效果/样式/触发器/动画。 :(有人可以指导我并纠正它。请让我理解背后的原因:(

1 个答案:

答案 0 :(得分:0)

<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="350" Width="525">
<Window.Resources>
    <Style x:Key="BorderlessButton" TargetType="{x:Type Button}">
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="border" Background="{TemplateBinding Background}">
                        <ContentPresenter Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  Margin="{TemplateBinding Padding}"
                                  RecognizesAccessKey="True"
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{DynamicResource BorderlessButton}">
        <Button.Content>
            <Ellipse Width="103" Height="107" Fill="Yellow" />
        </Button.Content>
    </Button>
</Grid>

自定义样式禁用系统动画动画。代码来自here