在鼠标输入动画后设置按钮颜色

时间:2014-03-05 12:38:27

标签: c# wpf xaml

我在鼠标悬停时有一个动画按钮可以改变背景和文字颜色,但我希望在点击后设置它,我的代码不起作用。

 <Button Style="{StaticResource ButtonProduct}" x:Name="Overview_Button" Click="Overview_Button_Click" FontFamily="pack://application:,,,/Images/Fonts/#HandelGothicEF" Width="198" Height="50"   Margin="0,30,0,20" FontSize="26">
    <TextBlock FontFamily="Univers LT Std 57 Cn" FontSize="16" Text="Overview" />
</Button>

通用XAML:

<!-- ButtonProduct -->
<Style x:Key="ButtonProduct" TargetType="Button">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="Border" CornerRadius="0" BorderThickness="0" Focusable="False" BorderBrush="Transparent" Background="White">
                    <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <EventTrigger RoutedEvent="MouseEnter">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation From="White" To="#52b0ca" Duration="0:0:0.5" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="MouseLeave">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation From="#52b0ca" To="White" Duration="0:0:0.5" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

代码:

private void Overview_Button_Click(object sender, RoutedEventArgs e)
        {
            var bc = new BrushConverter();
            Overview_Button.Background = (Brush)bc.ConvertFrom("#51b0ce");

            Clear_Main_Panel();
            overviewObj = new Overview(family, Product_CombBox.SelectedIndex);
            this.Center_Panel.Children.Add(overviewObj);
            isOverview = true;
        }

0 个答案:

没有答案