问题描述
我正在尝试实现选项卡样式的单选按钮.此样式包含一个文本块作为网格的子元素,选中时会更改其颜色,并且网格是边框元素的子元素.
i'm trying to implement a tab style radio button.this style contains a textblock as a child of a grid which changes it's color when checked and the grid is a child of border element.
我试图仅在网格处于选中状态时绑定它的背景但按钮最终是透明的
i'm trying to bind the grid's background only when it is in checked statebut the button end's up bieng transparent
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ContentPresenter" d:IsOptimized="True"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background)" Storyboard.TargetName="CheckedVisual">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="#ff9977"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked"/>
<VisualState x:Name="Indeterminate"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="PointerFocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid x:Name="CheckedVisual" Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
Opacity="{TemplateBinding Opacity}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}" Background="Transparent">
<ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" d:LayoutOverrides="TopPosition, BottomPosition" Opacity="0.6" RenderTransformOrigin="0.5,0.5">
<ContentPresenter.RenderTransform>
<CompositeTransform/>
</ContentPresenter.RenderTransform>
</ContentPresenter>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我什至尝试将 Grid x:Name="CheckedVisual" 的 backgroundproperty 绑定到 {TemplateBinding Background}
i've even tried binding the Grid x:Name="CheckedVisual" 's backgroundproperty to {TemplateBinding Background}
问题出在这部分代码
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ContentPresenter" d:IsOptimized="True"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background)" Storyboard.TargetName="CheckedVisual">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="{TemplateBinding Background}"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked"/>
<VisualState x:Name="Indeterminate"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
<VisualState x:Name="PointerFocused"/>
</VisualStateGroup>
检查单选按钮时,不透明度将按预期进行1,但颜色不变.我什至尝试过 {Binding Background} .. 但更改为 statoc 颜色适用于例如:#117755".
when the radio button is checked the opacity is going to 1 as expected but the colour isn't changing . i've even tried {Binding Background} .. but changing to statoc color works for eg: "#117755".
任何人都可以指导我..我的目标是在检查时更改网格的背景..并且颜色应该更改为背景的绑定值.
can anyone please guide me.. The end result i'm aiming at is changing the backgrund of the grid when it is checked stated .. and the color should be changed to the binded value of background.
推荐答案
根据文档,您不能这样做:https://msdn.microsoft.com/en-us/library/ms742868.aspx?f=255&MSPPError=-2147217396
You can't do that according to the docs: https://msdn.microsoft.com/en-us/library/ms742868.aspx?f=255&MSPPError=-2147217396
控件模板中的动画"
不能使用动态资源引用或数据绑定表达式设置情节提要或动画属性值.那是因为ControlTemplate 中的所有内容都必须是线程安全的,并且计时系统必须冻结 Storyboard 对象以使其线程安全.如果 Storyboard 或其子时间线包含动态资源引用或数据绑定表达式.更多有关冻结和其他 Freezable 功能的信息,请参阅可冻结对象概述.
你可以做的是在你的元素后面有一个单独的矩形/边框/东西,它的背景绑定到你想要的颜色,而不是动画它的不透明度.它看起来是一样的,但你不会遇到这个问题.
What you could do is to have a separate Rectangle/Border/Something behind your elements with its background bound to your wanted color and animate it's opacity instead. It'll look the same, but you won't run into this issue.
这篇关于动画时其他控件的数据绑定属性到样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!