我现在在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}
干杯,
克里斯
答案 0 :(得分: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;
}
}