如何参数化样式

本文关键字:样式 参数 何参数 | 更新日期: 2023-09-27 18:09:45

我是WPF新手。我试着做同样的风格。这个想法是有一个按钮样式可定制。所以,例如,我想改变按钮的背景颜色。或者按钮的图像。

下面是样式 的代码
<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    x:Class="WPFControlsApp.App"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- Resources scoped at the Application level should be defined here. -->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Simple Styles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <ControlTemplate x:Key="CustomButtonStyle" TargetType="{x:Type Button}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="CuandoEstoyArribaDelBoton">
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="Glow">
                            <EasingColorKeyFrame KeyTime="0" Value="White"/>
                            <EasingColorKeyFrame KeyTime="0:0:0.7" Value="#FF562020"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid>
                    <Rectangle x:Name="Background" Fill="#FF5A98EB" RadiusY="15" RadiusX="15" Stroke="#FF114FA1" StrokeThickness="2"/>
                    <Rectangle x:Name="Glow" Fill="White" RadiusY="15" RadiusX="15" Stroke="{x:Null}" StrokeThickness="0" Margin="0,0,0,49" Opacity="0.215"/>
                    <Rectangle x:Name="Glass" RadiusY="15" RadiusX="15" Stroke="#FF114FA1" StrokeThickness="2" Opacity="0.475">
                        <Rectangle.Fill>
                            <RadialGradientBrush RadiusY="0.62" RadiusX="0.62" GradientOrigin="0.506,1.063">
                                <GradientStop Color="#00000000" Offset="0"/>
                                <GradientStop Color="#FF00084B" Offset="1"/>
                            </RadialGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{Binding MINOMBREBOTON, FallbackValue=MiBoton}"/>
                    <Image HorizontalAlignment="Left" Margin="7,24,0,25" Width="100" Source="{Binding BtnImageSource}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsFocused" Value="True"/>
                    <Trigger Property="IsDefaulted" Value="True"/>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.ExitActions>
                            <StopStoryboard BeginStoryboardName="CuandoEstoyArribaDelBoton_BeginStoryboard"/>
                        </Trigger.ExitActions>
                        <Trigger.EnterActions>
                            <BeginStoryboard x:Name="CuandoEstoyArribaDelBoton_BeginStoryboard" Storyboard="{StaticResource CuandoEstoyArribaDelBoton}"/>
                        </Trigger.EnterActions>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True"/>
                    <Trigger Property="IsEnabled" Value="False"/>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ResourceDictionary>
    </Application.Resources>
</Application>

这是我不知道如何解决的问题

<Image HorizontalAlignment="Left" Margin="7,24,0,25" Width="100" Source="{Binding BtnImageSource}"/>

我有一个绑定名为BtnImageSource。但是我不知道如何在按钮定义中设置它。

<Button Content="Salir" Height="102" HorizontalAlignment="Right" Margin="0,0,25,26" Name="btn_salir" VerticalAlignment="Bottom" Width="280" ClipToBounds="False" Click="btn_salir_Click" Style="{StaticResource CustomButtonStyle}" />

你知道我怎么能有3到4个按钮,每个具有相同的风格,但不同的图像?

这是一个新应用程序的测试。我们的想法是使用带有参数的样式或寻找其他替代方法。

提前感谢。

如何参数化样式

是可能的....DynamicResource应该会有所帮助。将颜色属性设置为某些DynamicResource(带有资源键)。此密钥在申报时可能不存在。然后,当你需要一个特定的颜色,然后创建一个该颜色的画笔,并声明它相同的 Key,并与相关资源合并。

。你有这个样式资源,使用动态颜色画笔DynamicColorBrush设置为按钮的背景…

<App.Resources ...>
 <Style TargetType="{x:Type Button}">
    <Setter Background="{DynamicResource DynamicColorBrush}" />
 </Style>
.... 

注意App.Resources没有定义DynamicColorBrush。现在假设您在两个不同的视图

中定义了按钮
<UserControl ... x:Class="UserControl1">
     <UserControl.Resources>
          <SolidColorBrush x:Key="DynamicColorBrush" Color="Red"/>
     </UserControl.Resources>
     <Grid><Button Content="Red Button"/></Grid>
</UserControl> 

<UserControl ... x:Class="UserControl2">
     <UserControl.Resources>
          <SolidColorBrush x:Key="DynamicColorBrush" Color="Green"/>
     </UserControl.Resources>
     <Grid><Button Content="Green Button"/></Grid>
</UserControl>

应该可以。

允许运行时基本颜色变化的flexi主题的整个概念是使用动态资源概念完成的。