【问题标题】:Set Image Opacity with an DoubleAnimation使用 DoubleAnimation 设置图像不透明度
【发布时间】:2016-03-11 14:31:53
【问题描述】:

这应该相当简单,但我似乎无法正确完成。 我收藏了System.Windows.Controls.Image。当我选择其中一个时,我希望所有其他的不透明度为0.5。我正在使用 MVVM,其背后的逻辑(查找所选图像并将其设置为 Enabled)在 ViewModel 中完成,并且正在运行。所以基本上这是可行的:

<Image Grid.Row="0" Source="{Binding ItemImage}" IsEnabled="{Binding ItemImageEnabled}">
<Image.Style>
    <Style TargetType="Image">
        <Style.Triggers>
            <Trigger Property="IsEnabled"  Value="False">
                <Setter Property="Opacity" Value="0.5"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="True">
                <Setter Property="Opacity" Value="1.0"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Image.Style>
</Image>

现在我希望不透明度转换为动画,当未选择图像时将其从1.0 淡化到0.5,并在未选择图像时从0.5 淡化到1.0。我原以为这样会起作用:

<Image Grid.Row="0" Source="{Binding ItemImage}" IsEnabled="{Binding ItemImageEnabled}">
<Image.Style>
    <Style TargetType="Image">
        <Style.Triggers>
            <Trigger Property="IsEnabled"  Value="False">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0.5" BeginTime="0:0:0" Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="IsEnabled" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.5" To="1" BeginTime="0:0:0" Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</Image.Style>

...但它没有

任何人都有任何想法。任何帮助将不胜感激。提前致谢。

【问题讨论】:

    标签: c# wpf xaml mvvm doubleanimation


    【解决方案1】:

    您需要Trigger.ExitActions 而不是两个Trigger。这应该是你想要的:

    <Style TargetType="Image">
        <Style.Triggers>        
            <Trigger Property="IsEnabled" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.5" To="1" BeginTime="0:0:0" Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
                <Trigger.ExitActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0.5" BeginTime="0:0:0" Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.ExitActions>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 这是一种巧妙的方法,我打算让他制作一个 RadioButton 模板,这样一次只能选择一个。 +1
    猜你喜欢
    • 1970-01-01
    • 2010-11-09
    • 2011-09-08
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多