wpf 从代码隐藏更改不透明度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20268639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing opacity from code-behind
提问by Gerard
Why has the following event in a Window code-behind no effect?
为什么窗口代码隐藏中的以下事件无效?
void about_Click(object sender, RoutedEventArgs e)
{
// TopLevel.Opacity = 1.0, Splashscreen.Opacity = 0.0
TopLevel.Opacity = 0.1;
// still: TopLevel.Opacity = 1.0
Splashscreen.Opacity = 1.0;
// still: Splashscreen.Opacity = 0.0
}
The opacity values do not change.
不透明度值不会改变。
I discovered that the following Trigger is the cause of my issue:
我发现以下触发器是我的问题的原因:
<Window.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard Storyboard="{StaticResource splashscreenanimation}" />
</EventTrigger>
</Window.Triggers>
When I remove it the code-behind is working.
当我删除它时,代码隐藏正在工作。
For completeness, this is the animation:
为了完整起见,这是动画:
<Window.Resources>
<Storyboard x:Key="splashscreenanimation">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="Splashscreen"
BeginTime="0:0:0.900">
<EasingDoubleKeyFrame KeyTime="0:0:1.5"
Value="0" />
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="TopLevel"
BeginTime="0:0:0.900">
<EasingDoubleKeyFrame KeyTime="0:0:1.5"
Value="1" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</Window.Resources>
Solution: In code behind you can remove the animation by first executing: Splashscreen.BeginAnimation(UserControl.OpacityProperty, null);
(Splashscreen is a UserControl).
I also tried adding FillBehavior="HoldEnd"or FillBehavior="Stop"to the Storyboard but did'nt get it to work properly.
解决方案:在后面的代码中,您可以通过首先执行来移除动画: Splashscreen.BeginAnimation(UserControl.OpacityProperty, null);
(Splashscreen 是一个 UserControl)。
我也尝试将FillBehavior="HoldEnd"或添加FillBehavior="Stop"到故事板,但没有让它正常工作。
回答by dev hedgehog
Again you and again the same issue with dependency property value precedence.
依赖属性值优先级再次出现同样的问题。
Take a look at the priorities.
看看优先级。
Property system coercion.
Active animations, or animations with a Hold behavior. In order to have any practical effect, an animation of a property must be able to have precedence over the base (unanimated) value, even if that value was set locally.
Local value. A local value might be set through the convenience of the "wrapper" property, which also equates to setting as an attribute or property element in XAML, or by a call to the SetValue API using a property of a specific instance.
财产制度强制。
活动动画或具有保持行为的动画。为了产生任何实际效果,属性的动画必须能够优先于基本(未动画)值,即使该值是在本地设置的。
当地价值。可以通过方便的“包装器”属性设置本地值,这也等同于在 XAML 中设置为属性或属性元素,或者通过使用特定实例的属性调用 SetValue API。
In your case animation takes over.
在你的情况下,动画接管了。
Your code here is #3. You are setting the local value but animation still takes over.
你在这里的代码是#3。您正在设置本地值,但动画仍然接管。
void about_Click(object sender, RoutedEventArgs e)
{
// TopLevel.Opacity = 1.0, Splashscreen.Opacity = 0.0
TopLevel.Opacity = 0.1;
// still: TopLevel.Opacity = 1.0
Splashscreen.Opacity = 1.0;
// still: Splashscreen.Opacity = 0.0
}
I hope now you finally understand how priorities work. :) :) :)
我希望现在你终于明白优先级是如何工作的。:) :) :)

