【问题标题】:Apply the same style to multiple buttons in a XAML page [duplicate]将相同样式应用于 XAML 页面中的多个按钮 [重复]
【发布时间】:2019-10-01 09:58:04
【问题描述】:

每当单击按钮时,我都会尝试向按钮添加背景颜色,并在一段时间后恢复其原始颜色。我尝试创建 ResourceDictionary.xaml 并添加了我的样式集并将其引用添加到 XAML 页面。我的代码似乎有问题。如果我将样式直接添加到按钮标签中,它可以工作.. 但不是通过 ResourceDictionary 。请帮忙。

ResourceDictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FantasyPlay">

<Style TargetType="Button">
    <Setter Property="Background" Value="Gainsboro" />
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" From="Orange" To="Gainsboro" Duration="0:0:0.50" AutoReverse="True" />
                        <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" From="Gainsboro"  To="Orange" Duration="0:0:0.1" AutoReverse="True" />
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

MyXAML:

我已经引用它像

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="ResourceDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

【问题讨论】:

    标签: c# wpf xaml resourcedictionary


    【解决方案1】:

    美好的一天,让我们试试这个

    <Trigger Property="IsPressed" Value="True">
                    <Trigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard  AutoReverse="True" >
                                <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" Duration="0:0:0"/>
                                <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Orange" Duration="0:0:0.50"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.ExitActions>
                </Trigger>
    

    【讨论】:

    • 谢谢@Daniil Rudchenko。这就像一个魅力和一个简单的!
    【解决方案2】:

    WPF 控件有一个模板。

    按钮的模板做了很多事情,其中​​之一是鼠标悬停效果。这会干扰你的造型。

    这是一种与您的类似的样式,但替换了按钮模板,因此您可以在鼠标悬停时看到效果。

    我使用的是红色而不是 Gainsboro,因此这可能不是一个完整的剪切和粘贴解决方案,但可以帮助您入门。

        <Style TargetType="Button">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Name="border" 
                                BorderThickness="1"
                                Padding="4,2" 
                                BorderBrush="DarkGray" 
                                CornerRadius="3" 
                                Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Black" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" 
                                                To="Orange" 
                                                Duration="0:0:1" 
                                                AutoReverse="True" 
                                                FillBehavior="Stop" />
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    

    显然,这会替换您在资源字典中的内容。请先保留一份原件。

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      • 2013-03-15
      • 1970-01-01
      相关资源
      最近更新 更多