将其他控件的数据绑定到样式,同时为其设置动画

时间:2015-06-10 18:43:17

标签: c# windows-runtime windows-phone-8.1

我试图实现标签式单选按钮。 此样式包含一个文本块作为网格的子项,在选中时会更改它的颜色,并且网格是border元素的子项。

我试图仅在网格处于检查状态时绑定网格的背景 但按钮结束透明

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

                                </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>

我甚至尝试绑定网格x:名称=&#34; CheckedVisual&#34; {TemplateBinding Background}

的backgroundproperty

问题出在代码的这一部分

<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,但颜色不会改变。我甚至试过{绑定背景} ..但改为statoc颜色适用于例如:&#34;#117755&#34;。

任何人都可以指导我..最终结果我的目标是在检查时声明改变网格背景。并且颜色应该更改为背景的绑定值。

1 个答案:

答案 0 :(得分:1)

根据文档https://msdn.microsoft.com/en-us/library/ms742868.aspx?f=255&MSPPError=-2147217396

,您无法做到这一点

&#34;在ControlTemplate中进行动画制作&#34;

  

您无法使用动态资源引用或数据绑定表达式   设置Storyboard或动画属性值。那是因为   ControlTemplate中的所有内容都必须是线程安全的,并且   计时系统必须冻结Storyboard对象才能使它们具有线程安全性。   如果故事板或其子时间轴包含,则无法冻结该故事板   动态资源引用或数据绑定表达式。更多   有关冻结和其他Freezable功能的信息,请参阅   Freezable Objects概述。

你可以做的是在你的元素背后有一个单独的矩形/边框/东西,背景绑定到你想要的颜色,而不是为它设置不透明度。它看起来一样,但你不会遇到这个问题。