UWP ToggleButton图片取决于IsChecked

时间:2017-09-05 21:07:36

标签: c# xaml uwp win-universal-app

我试图根据ToggleButton的{​​{1}}状态为IsChecked提供不同的图片,但在UWP Style触发器中不再存在,因此必须在另一种方式。

我讨论了所谓的解决方案是使用ToggleButton,但我无处可去。

我想要实现的目标(未选中蓝色,检查绿色):

enter image description here

enter image description here

以下是好方向吗?

DataTriggerBehavior

或者有更好/更简单的方法来实现这一目标吗?我还读了一些关于VisualStateManager的东西,我也没有经验......

希望有人能把我推向正确的方向......

1 个答案:

答案 0 :(得分:4)

试试这个

<ToggleButton x:Name="ToggleButton"
                IsChecked="{Binding SignalButtonSelected, Mode=TwoWay}"
                Command="{Binding SignalButtonCommand}">
    <Grid>
        <Image x:Name="IsCheckedImage"
                Source="Images/Buttons/Button signal.png">
            <Interactivity:Interaction.Behaviors>
                <Core:DataTriggerBehavior Binding="{Binding IsChecked, ElementName=ToggleButton}"
                                            Value="True">
                    <Core:ChangePropertyAction TargetObject="{Binding ElementName=IsCheckedImage}"
                                                PropertyName="Visibility"
                                                Value="Collapsed" />
                    <Core:ChangePropertyAction TargetObject="{Binding ElementName=IsUncheckedImage}"
                                                PropertyName="Visibility"
                                                Value="Visible" />
                </Core:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Image>
        <Image x:Name="IsUncheckedImage"
                Source="Images/Buttons/Button electric ns.png">
            <Interactivity:Interaction.Behaviors>
                <Core:DataTriggerBehavior Binding="{Binding IsChecked, ElementName=ToggleButton}"
                                            Value="False">
                    <Core:ChangePropertyAction TargetObject="{Binding ElementName=IsCheckedImage}"
                                                PropertyName="Visibility"
                                                Value="Visible" />
                    <Core:ChangePropertyAction TargetObject="{Binding ElementName=IsUncheckedImage}"
                                                PropertyName="Visibility"
                                                Value="Collapsed" />
                </Core:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Image>
    </Grid>
</ToggleButton>