简体   繁体   English

DynamicResource颜色不起作用

[英]DynamicResource color not working

I have the following code: 我有以下代码:

<Color x:Key="SelectedColor">Gold</Color> 

And a TabItem Style that contains the color 以及包含颜色的TabItem样式

<VisualState x:Name="Selected">
    <Storyboard>
        <ColorAnimationUsingKeyFrames 
            Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)"
            Storyboard.TargetName="InnerRectangle2">
            <EasingColorKeyFrame KeyTime="0" Value="{DynamicResource SelectedColor}"/>
        </ColorAnimationUsingKeyFrames>

It turns out I can't use a DynamicResource on an EasingColorKeyFrame . 事实证明我不能在EasingColorKeyFrame上使用DynamicResource
What can I do to achieve my effect? 我能做些什么来达到我的效果?

I need to set the color dynamically, so just swapping "{DynamicResource SelectedColor}" with "{StaticResource SelectedColor}" is off the table. 我需要动态设置颜色,所以只需用"{StaticResource SelectedColor}"交换"{DynamicResource SelectedColor}"

I've created a tiny solution to demonstrate the problem - the Selected Tab should be Gold colored, but it's actually transparent, because I guess the VSM can't resolve the color named " SelectedColor " 我已经创建了一个很小的解决方案来演示这个问题 - Selected Tab应该是Gold color,但它实际上是透明的,因为我猜VSM无法解析名为“ SelectedColor ”的颜色

http://dl.dropbox.com/u/10557283/DynamicBug.zip http://dl.dropbox.com/u/10557283/DynamicBug.zip

Animations(VSM) are freezable objects . 动画(VSM)是freezable objects As soon as you set a binding on a dependency property of a Freezable, you prevent the Freezable from being frozen. 只要在Freezable的依赖项属性上设置绑定,就可以防止Freezable被冻结。 Thus, the bindings on the Value property of your EasingColorKeyFrame objects are preventing the storyboards from being frozen. 因此, EasingColorKeyFrame objects are preventing the storyboards from being frozen.的Value属性上的绑定EasingColorKeyFrame objects are preventing the storyboards from being frozen.

As a way out you can try any these three approaches whichever suits you best - 作为一种出路,您可以尝试任何这三种方法,无论哪种方式最适合您 -

  • Try declaring the resource as StaticResource and use it in your VSM. 尝试将资源声明为StaticResource并在VSM中使用它。 StaticResource explanation for VSM VSM的StaticResource解释

  • What i understand from your code is you want the selected tabItem in golden color. 我从你的代码中理解的是你想要选择的tabItem为金色。 So, as a workaround you can do is have the two borders contained in a panel say grid one over other with golden border default visibility as collapsed and normal one visible. 因此,作为一种解决方法,您可以做的是将面板中包含的two borders称为网格一个,其中金色边框默认visibility为折叠,正常可见。 Now, on selected event (on property change of IsSelected or whatever aproach) of your tabItem you can swap the visibility of two borders giving the same effect. 现在,在tabItem的selected event (在IsSelected或任何方法的属性更改)上,您可以swap the visibility两个边框swap the visibility ,从而产生相同的效果。 Of course this workaround is specific to this case, for example it only makes sense if the EasingColorKeyFrame key time is 0, otherwise it doesn't give the same visual effect. 当然,这种解决方法特定于这种情况,例如,只有当EasingColorKeyFrame键时间为0时才有意义,否则它不会产生相同的视觉效果。

  • Lastly, if you want to stick to do it through animation, you can achieve this in code behind . 最后,如果你想坚持通过动画来做,你可以在code behind实现这一点。 These posts might proves helpful to you - Woakaround for dynamicResource in Animation , Animation in code behind and Setting foreground with VSM 这些帖子可能对您有所帮助 - 动画中的 dynamicResource, 后面的代码 中的动画使用VSM设置前景的 Woakaround

这是因为VSM类型不是逻辑树的一部分,因此无法解析动态资源查找。

I figured out a way to do it with layers. 我想出了一种用图层做的方法。 Make multiple copies of your object, and then just modify the transparencies like this: 制作对象的多个副本,然后只需修改透明度,如下所示:

<VisualState x:Name="Selected">
    <Storyboard>
        <DoubleAnimation Storyboard.TargetName="InnerRectangleBorder"
                         Storyboard.TargetProperty="Opacity"
                         To="0"
                         Duration="0:0:0" />

        <DoubleAnimation Storyboard.TargetName="InnerRectangleBorderMouseOver"
                         Storyboard.TargetProperty="Opacity"
                         To="0"
                         Duration="0:0:0.5" />

        <DoubleAnimation Storyboard.TargetName="InnerRectangleBorderSelected"
                         Storyboard.TargetProperty="Opacity"
                         To="1"
                         Duration="0:0:1" />
    </Storyboard>
</VisualState>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM