【问题标题】:data binding property of a other control to a style while animating it动画时将其他控件的数据绑定属性绑定到样式
【发布时间】:2015-08-26 04:50:18
【问题描述】:

我正在尝试实现一个选项卡样式的单选按钮。 此样式包含一个文本块作为网格的子元素,选中后会更改其颜色,并且网格是边框元素的子元素。

我正在尝试仅在处于选中状态时绑定网格的背景 但是按钮结束了 bieng 透明

                                </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" 的背景属性绑定到 {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”。

谁能指导我..我的最终结果是在检查时更改网格的背景..并且颜色应该更改为背景的绑定值。

【问题讨论】:

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


    【解决方案1】:

    根据文档,您不能这样做:https://msdn.microsoft.com/en-us/library/ms742868.aspx?f=255&MSPPError=-2147217396

    “在 ControlTemplate 中制作动画”

    您不能使用动态资源引用或数据绑定表达式 设置情节提要或动画属性值。那是因为 ControlTemplate 中的所有内容都必须是线程安全的,并且 计时系统必须冻结 Storyboard 对象以使其成为线程安全的。 如果 Storyboard 或其子时间线包含 动态资源引用或数据绑定表达式。更多 有关冻结和其他 Freezable 功能的信息,请参阅 可冻结对象概述。

    你可以做的是在你的元素后面有一个单独的 Rectangle/Border/Something,它的背景绑定到你想要的颜色,而不是动画它的不透明度。它看起来是一样的,但你不会遇到这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-20
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多