你应该在你的viewmodel(播放/停止)中定义播放状态,并使用转换器将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}"
/>
来源
2011-08-19 15:09:23
alf
谢谢。如果你碰到一个例子,说明如何在演示项目中实现这个功能,请下载像这样的下载。谢谢。 –
不客气!我编辑了这个问题,并附上了一个关于如何做到这一点的例子。希望能帮助到你。 – alf
现在它清澈透明。谢谢! –