代码之家  ›  专栏  ›  技术社区  ›  bas

UWP切换按钮图像取决于已检查

  •  1
  • bas  · 技术社区  · 7 年前

    ToggleButton 不同的图像取决于 IsChecked 切换按钮 ,但在UWP中,触发器不再存在,因此必须以不同的方式进行。

    DataTriggerBehavior

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

    enter image description here

    enter image description here

    <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: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=IsUncheckedImage}" PropertyName="Visibility" Value="Collapsed" />
                </Core:DataTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
        </Image>
        </Grid>
    </ToggleButton>
    

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

    1 回复  |  直到 7 年前
        1
  •  4
  •   Jessica    7 年前

    试试这个

    <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>