c#-在MVVM模式中单击时更改按钮颜色
作者:互联网
我有一个ICommand,当按下按钮时会触发该ICommand.发生这种情况时,我需要更改此按钮的背景颜色.如何使用MVVM模式执行此操作?
private DelegateCommand asioMuted;
public ICommand AsioMuted
{
get
{
if (asioMuted == null)
{
asioMuted = new DelegateCommand(MuteAsio);
}
return asioMuted;
}
}
XAML:
<Button Content="Mute"
Command="{Binding AsioMuted}"
HorizontalAlignment="Left"
Margin="29,282,0,0"
VerticalAlignment="Top"
Width="42"
RenderTransformOrigin="-0.273,1.818" />
解决方法:
将按钮的背景色绑定到ViewModel上的brush属性.然后,让ICommand更改视图模型上画笔的颜色.有些人可能还建议将颜色绑定到ViewModel的IsMuted属性,然后使用转换器将true / false转换为画笔.这种方法从ViewModel中删除所有特定于UI的细节.
标签:icommand,wpf,xaml,c,mvvm 来源: https://codeday.me/bug/20191030/1967071.html