您应该在视图模型中定义播放状态(播放/停止),并使用转换器将 Button.Style 绑定到该属性。在您的转换器中,根据当前状态返回不同的样式(取自 App.Current.Resources)。
编辑:
以下是您的转换器示例:
public class StateStyleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (PlaybackState)value == PlaybackState.Playing ? App.Current.Resources["RoundPlay"] : App.Current.Resources["RoundStop"];
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
在本例中,PlaybackState 是一个枚举:
public enum PlaybackState
{
Playing,
Stopped
}
然后您应该将 state 属性添加到您的视图模型(通知更改的部分取决于您用于 MVVM 的框架):
private PlaybackState state;
public PlaybackState State
{
get { return state; }
set
{
state = value;
RaiseNotifyPropertyChanged("State");
}
}
在 XAML 中声明您的转换器:
<UserControl.Resources>
<converters:StateStyleConverter x:Key="StateStyleConverter"/>
</UserControl.Resources>
最后绑定到按钮上:
<Button Width="72" Height="72" Style="{Binding State, Converter={StaticResource StateStyleConverter}}"
DataContext="{Binding ElementName=this, Path=DataContext}"
cmd:ButtonBaseExtensions.Command="{Binding PlayStopCommand}"
/>