在 XAML 中设置控件模板的属性
我创建了一个按钮控件模板,我想在 xaml 级别更改按钮的发光颜色。 这就是我的意思。
<Button x:Name="btnClose" Template="{DynamicResource GlassButton}" Width="32" Height="32" GlowStartColor="Red" GlowEndColor="DarkRed">Button1</Button>
GlowStartColor 和 GlowEndColor 是我想要在控件模板上更改的属性。
这是我的控件模板
<ControlTemplate x:Key="DefaultGlassButton" TargetType="{x:Type Button}">
<!-- Internal Resources -->
<ControlTemplate.Resources>
<Storyboard x:Key="Storyboard1" RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Glow" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.7000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Glow" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#00C080FF"/>
<SplineColorKeyFrame KeyTime="00:00:00.7000000" Value="#00C080FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Storyboard2">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Glow" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0.745"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<!-- Actual Control Template-->
<Border x:Name="OuterBorder" BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">
<Border x:Name="InnerBorder" Background="#7F000000" BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Border x:Name="Glow" HorizontalAlignment="Stretch" Width="Auto" Opacity="0" Grid.RowSpan="2" CornerRadius="4,4,4,4">
<Border.Background>
<RadialGradientBrush>
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.852" ScaleY="2.281"/>
<SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.5"/>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
<TranslateTransform X="0.014" Y="0.458"/>
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop x:Name="GlowStartColor" Color="#B1FF80FF" Offset="0"/>
<GradientStop x:Name="GlowEndColor" Color="#00C080FF" Offset="1"/>
</RadialGradientBrush>
</Border.Background>
</Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2"/>
<Border x:Name="Shine" HorizontalAlignment="Stretch" Margin="0,0,0,0" CornerRadius="4,4,0,0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop x:Name="ShineStartColor" Color="#99FFFFFF" Offset="0"/>
<GradientStop x:Name="ShineEndColor" Color="#26FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
</Border>
<!-- Triggers -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard x:Name="Storyboard2_BeginStoryboard" Storyboard="{StaticResource Storyboard2}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
<Setter Property="Background" TargetName="InnerBorder" Value="#CC000000"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
这是执行此操作的正确方法,还是有更好的方法? 对于 WPF 和 XAML 来说还相当陌生,因此仍然有很多学习要做。
I've created a button control template where I want to change the glow color of the button on the xaml level. Here is what I mean.
<Button x:Name="btnClose" Template="{DynamicResource GlassButton}" Width="32" Height="32" GlowStartColor="Red" GlowEndColor="DarkRed">Button1</Button>
The GlowStartColor and GlowEndColor are the properties I want to change on the control template.
Here's my control template
<ControlTemplate x:Key="DefaultGlassButton" TargetType="{x:Type Button}">
<!-- Internal Resources -->
<ControlTemplate.Resources>
<Storyboard x:Key="Storyboard1" RepeatBehavior="Forever" AutoReverse="True">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Glow" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
<SplineDoubleKeyFrame KeyTime="00:00:00.7000000" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Glow" Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)">
<SplineColorKeyFrame KeyTime="00:00:00.3000000" Value="#00C080FF"/>
<SplineColorKeyFrame KeyTime="00:00:00.7000000" Value="#00C080FF"/>
</ColorAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="Storyboard2">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Glow" Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0.745"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</ControlTemplate.Resources>
<!-- Actual Control Template-->
<Border x:Name="OuterBorder" BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">
<Border x:Name="InnerBorder" Background="#7F000000" BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Border x:Name="Glow" HorizontalAlignment="Stretch" Width="Auto" Opacity="0" Grid.RowSpan="2" CornerRadius="4,4,4,4">
<Border.Background>
<RadialGradientBrush>
<RadialGradientBrush.RelativeTransform>
<TransformGroup>
<ScaleTransform CenterX="0.5" CenterY="0.5" ScaleX="1.852" ScaleY="2.281"/>
<SkewTransform AngleX="0" AngleY="0" CenterX="0.5" CenterY="0.5"/>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
<TranslateTransform X="0.014" Y="0.458"/>
</TransformGroup>
</RadialGradientBrush.RelativeTransform>
<GradientStop x:Name="GlowStartColor" Color="#B1FF80FF" Offset="0"/>
<GradientStop x:Name="GlowEndColor" Color="#00C080FF" Offset="1"/>
</RadialGradientBrush>
</Border.Background>
</Border>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2"/>
<Border x:Name="Shine" HorizontalAlignment="Stretch" Margin="0,0,0,0" CornerRadius="4,4,0,0">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop x:Name="ShineStartColor" Color="#99FFFFFF" Offset="0"/>
<GradientStop x:Name="ShineEndColor" Color="#26FFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
</Border>
<!-- Triggers -->
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource Storyboard1}"/>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard x:Name="Storyboard2_BeginStoryboard" Storyboard="{StaticResource Storyboard2}"/>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Opacity" TargetName="Shine" Value="0.4"/>
<Setter Property="Background" TargetName="InnerBorder" Value="#CC000000"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Is this the proper way to do this, or is there a better way? Still fairly new to WPF and XAML so still have alot of learning to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要子类化 Button 并添加这两个属性,或者将这两个属性添加为附加属性(通过创建一个具有两个附加依赖属性的新类)。
You need to either subclass Button and add those two properties or add those two properties as attached properties (by creating a new class with two attached dependency properties).
我最近不得不做类似的事情。 事实证明,您可以利用控件上已定义的其他属性。 因此,如果您的新模板不使用
TemplateBinding
来表示Foreground
和Background
,那么您可以将开始/停止发光颜色粘贴到那里。 所有控件上还有一个通常为空的Tag
属性。值得研究这些选项作为子类化控件的替代方法。 在控件模板只有一个自定义参数的情况下,重用现有属性通常是简单且自然的。
I had to do something similar recently. Turns out that you can piggyback off of other properties that are already defined on the control. So, if your new template does not use
TemplateBinding
s for, say,Foreground
andBackground
then you can stick your start/stop glow colours in there. There's also theTag
property on all controls that's usually empty.It's worth looking into these options as an alternative to subclassing a control. In cases where there is only one custom parameter to the control template, it's usually simple and often natural to reuse an existing property.
据我了解,您希望将模板中的发光颜色绑定到控件上的发光属性。 你可以这样做:
As far as I understand the question you want to bind glow colors in your template to glow properties on your control. You can do it this way:
我也无法使上述工作正常。 但我认为它走在正确的轨道上。 我只是想在定义按钮时能够设置发光属性。 执行此操作的唯一其他方法是制作按钮的自定义控件,但仅添加一些额外的属性似乎有点过多。
I couldn't get the above to work right either. Its on the right track though I think. I just wanted to have the ability to set the glow properties when I define the button. The only other way to do this is to make a custom control of a button, but it seems a little excessive just to add on some extra properties.