绑定刷新动画上的所有绑定控件

时间:2013-02-09 12:10:25

标签: c# wpf xaml binding

使用类似

的颜色装订时
Background="{Binding Design.LeftBarColor}"

并执行类似

的动画
<DoubleAnimation From="1" To="0.5" Storyboard.TargetName="appName"
    Storyboard.TargetProperty="(Label.Background).(SolidColorBrush.Opacity)"
    Duration="0:0:0.25"/>

刷新所有绑定到“Design.LeftBarColor”的控件。但我只想刷新标签的(appName)背景颜色。我只是尝试更改绑定模式,但这不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:2)

您可以为每个Label创建一个新的背景画笔,并将新的画笔颜色绑定到Design.LeftBarColor,而不是直接使用Design.LeftBarColor.Color画笔作为标签的背景。

<Label Name="appName" ...>
    <Label.Background>
        <SolidColorBrush Color="{Binding Design.LeftBarColor.Color}"/>
    </Label.Background>
   ...
</Label>

答案 1 :(得分:1)

不确定我对你要做的事情是否正确。我想说如果你想淡出动画控件,不要瞄准画笔的不透明度。将边框控件放在标签的顶部,然后修改边框的不透明度。

示例代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="MyBrush" Color="Red"/>
    </Window.Resources>
    <Grid>
        <Border Name="Container">
            <Label Background="{StaticResource MyBrush}">
                <Label.Triggers>
                    <EventTrigger RoutedEvent="Rectangle.Loaded">
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
            Storyboard.TargetName="Container" 
            Storyboard.TargetProperty="Opacity"
            From="1.0" To="0.0" Duration="0:0:5" 
            AutoReverse="True" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Label.Triggers>
                Lorem ipsum
            </Label>
        </Border>
    </Grid>
</Window>