嗨,我想将切换按钮与视图模型绑定。下面是视图代码
<ToggleButton Content="Toggle" Width="80">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding Path=CheckedCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding Path=UncheckedCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ToggleButton>
谁能帮我提供相应的视图模型代码?
你在使用棱镜吗?如果没有,您还需要在此类或 BaseViewModel 类中实现 DelegateCommand。
public class YourViewModel
{
private DelegateCommand _checkedCommand;
public DelegateCommand CheckedCommand
{
get
{
return _checkedCommand ?? (_checkedCommand = new DelegateCommand(OnCheckedCommand));
}
}
private DelegateCommand _uncheckedCommand;
public DelegateCommand UncheckedCommand
{
get
{
return _uncheckedCommand ?? (_uncheckedCommand = new DelegateCommand(OnUncheckedCommand));
}
}
private void OnUncheckedCommand() { }
private void OnCheckedCommand() { }
}