我有一个 ICommand
,它在按下 Button
时触发。发生这种情况时,我需要更改此 Button
的背景颜色。我如何使用 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" />
最佳答案
将按钮的背景颜色绑定(bind)到 ViewModel 上的画笔属性。然后,让 ICommand 更改 View 模型上画笔的颜色。有些人可能还建议将颜色绑定(bind)到 ViewModel 上的 IsMuted 属性,然后使用转换器将 true/false 转换为画笔。这种方法会从 ViewModel 中删除任何特定于 UI 的细节。
关于c# - 在 MVVM 模式中单击时更改按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19261480/