【问题标题】:XAML ToggleButton change background image with isChecked={Binding xyz}XAML ToggleButton 使用 isChecked={Binding xyz} 更改背景图像
【发布时间】:2016-04-04 16:14:20
【问题描述】:

我的 app.xaml 现在有以下工作代码...

<Style x:Key="likeActionButton" TargetType="ToggleButton">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal">
                                    </VisualState>
                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetName="HoverBackground"
                                                Storyboard.TargetProperty = "Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetName="PressedBackground"
                                                Storyboard.TargetProperty = "Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border>
                                <Grid>
                                    <Image Width="25" Source="ms-appx:///Assets\ActionIcons\like-action.png"></Image>
                                    <Image x:Name="HoverBackground" Width="25" Source="ms-appx:///Assets\ActionIcons\like-action-onHover.png" Visibility="Collapsed"></Image>
                                    <Image x:Name="PressedBackground" Width="25" Source="ms-appx:///Assets\ActionIcons\like-action-on-pressed.png" Visibility="Collapsed"></Image>
                                </Grid>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

我正在调用这个模板:

<ToggleButton Grid.Column="4" HorizontalAlignment="Center" 
                                          Style="{StaticResource likeActionButton}" 
                                          IsChecked="{Binding LikeState}"  
                                          Tapped="Favourite_Tapped"></ToggleButton>

LikeState 的绑定没有我想要的那么完美。

这很难解释,但我会试一试...

我可以选择和取消选择ToggleButton,背景图像将始终翻转。 LikeState 后面的绑定似乎适用于属性,但不适用于图像。这意味着当数据加载和布尔值LikeState = true 属性ToggleButton.IsChecked = true 但背景图像是VisualState x:Name="Normal" 的图像。

再说一遍…… 我正在使用LikeState = true 加载数据。如果我第一次单击 ToggleButton,背景图像不会改变,但代码隐藏文件会执行 isChecked = true 的代码 第二次单击 ToggleButton 时,背景图像现在会更改为 VisualState x:Name="Pressed"

那么我必须做些什么来设置正确的背景图像依赖于动态填充的属性isChecked={Binding LikeState}

干杯,

克里斯

【问题讨论】:

  • 尝试 Mode=TwoWay 绑定

标签: c# wpf xaml windows-phone-8.1


【解决方案1】:

在绑定中尝试Mode=TwoWay。 由于您在代码中更改有界属性值,因此要反映这一点,您必须将 Mode 设置为 TwoWay

更新

我已经检查了您的代码。无需双向绑定即可正常工作。

使用视觉状态选中

<VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="PointerOver"/>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled">
                                      </VisualState>
                                    <VisualState x:Name="Checked">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                Storyboard.TargetName="PressedBackground"
                                                Storyboard.TargetProperty = "Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                                            </ObjectAnimationUsingKeyFrames>
                                           </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="CheckedPointerOver">

                                    </VisualState>
                                    <VisualState x:Name="CheckedPressed"/>
                                    <VisualState x:Name="CheckedDisabled">
                                    </VisualState>
                                    <VisualState x:Name="Indeterminate"/>
                                    <VisualState x:Name="IndeterminatePointerOver"/>
                                    <VisualState x:Name="IndeterminatePressed"/>
                                    <VisualState x:Name="IndeterminateDisabled">
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

希望您为 LikeState 属性实施 INotifyPropertyChanged,以便最初检查它。如果没有,请执行以下操作。这就是我所做的

 public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        bool likeState=true;
        public bool LikeState 
        {
            get { return likeState; }
            set
            {
                if(value!=likeState)
                {
                    value = likeState;
                    OnPropertyChanged("LikeState");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        void OnPropertyChanged(string propertyName)
        {
           if(this.PropertyChanged!=null)
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));

        }
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
            LikeState = true;
            toggle.DataContext = this;

        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多