【问题标题】:How can I set an Image's effect via trigger on its parent's IsChecked property?如何通过触发器在其父级的 IsChecked 属性上设置图像的效果?
【发布时间】:2014-04-23 21:54:26
【问题描述】:

在我看来,我有几个切换按钮。他们每个人都有一个不同来源的图像作为其内容,并且它的 IsChecked 属性绑定到我的视图模型中的一个属性。我想向每个按钮的图像添加/删除 DropShadowEffect,具体取决于其按钮是否为 IsChecked。我可以为每个图像赋予自己的样式,在 MyProperty1、MyProperty2 等上使用 DataTrigger,但我也可以对所有图像使用单一样式吗?

<ToggleButton IsChecked="{Binding MyProperty1}" >
    <Image Source="{Binding MyIcon1}">
        <Image.Effect>
            <DropShadowEffect Direction="-45" ShadowDepth="3" />
        </Image.Effect>
    </Image>
</ToggleButton>

<ToggleButton IsChecked="{Binding MyProperty2}" >
    <Image Source="{Binding MyIcon2}">
        <Image.Effect>
            <DropShadowEffect Direction="-45" ShadowDepth="3" />
        </Image.Effect>
    </Image>
</ToggleButton>

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    您可以为Image 创建通用样式并在ToggleButton.IsChecked 属性上使用DataTrigger

    <Window.Resources>
        <Style TargetType="{x:Type Image}" x:Key="imageButtonStyle">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Direction="-45" ShadowDepth="3" />
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}, Path=IsChecked}" Value="True">
                    <Setter Property="Effect" Value="{x:Null}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    
    <ToggleButton IsChecked="{Binding MyProperty2}" >
        <Image Source="{Binding MyIcon2}" Style="{StaticResource imageButtonStyle}"/>
    </ToggleButton>
    

    【讨论】:

      猜你喜欢
      • 2013-04-09
      • 2010-12-17
      • 2016-01-13
      • 1970-01-01
      • 2011-03-10
      • 2013-08-30
      • 2012-11-23
      • 1970-01-01
      • 2011-07-14
      相关资源
      最近更新 更多