C# 在启动时设置按钮的ToggleState并检查状态
我的wpf应用程序中有一个切换按钮。启动时,必须设置togglebutton 我的Xaml文件:C# 在启动时设置按钮的ToggleState并检查状态,c#,wpf,mvvm,togglebutton,C#,Wpf,Mvvm,Togglebutton,我的wpf应用程序中有一个切换按钮。启动时,必须设置togglebutton 我的Xaml文件: <ToggleButton Content="AUD Reset" IsChecked="True" Height="23" HorizontalAlignment="Center" Margin="0" Name="button4" Command="{Binding Path=ConnectCommand}" VerticalAlignment="Center" Width="100" /
<ToggleButton Content="AUD Reset" IsChecked="True" Height="23" HorizontalAlignment="Center" Margin="0" Name="button4" Command="{Binding Path=ConnectCommand}" VerticalAlignment="Center" Width="100" />
在togglebutton上,单击我要检查viewmodel类中的切换状态,如果返回true,则我要执行以下操作:
我的ViewModel类:
private ICommand mUpdater;
public ICommand ConnectCommand
{
get
{
if (mUpdater == null)
mUpdater = new DelegateCommand(new Action(ConnectToSelectedDevice), new Func<bool>(ConnectCanExecute));
return mUpdater;
}
set
{
mUpdater = value;
}
}
public bool ConnectCanExecute()
{
return true;
}
public void ConnectToSelectedDevice()
{
mComm.SetAddress(0x40);
Byte[] buffer= new Byte[2];
buffer[0] = 0x24;
buffer[1] = 0x00;
if(Check if button togglestate is set, if true then)
{
buffer[1] = 0x04;
}
mComm.WriteBytes(2, buffer);
}
private ICommand mUpdater;
公共ICommand ConnectCommand
{
得到
{
if(mUpdater==null)
mUpdater=newdelegateCommand(新操作(connecttoselectedevice)、新函数(ConnectCanExecute));
返回mUpdater;
}
设置
{
mUpdater=值;
}
}
公共bool ConnectCanExecute()
{
返回true;
}
public void ConnectToSelectedDevice()
{
mComm.SetAddress(0x40);
字节[]缓冲区=新字节[2];
缓冲区[0]=0x24;
缓冲区[1]=0x00;
如果(检查是否设置了按钮切换状态,如果为真则)
{
缓冲区[1]=0x04;
}
mComm.WriteBytes(2,缓冲区);
}
如何在viewmodel中检查是否选中了togglebutton并执行上述语句
请帮忙 您可以将IsChecked属性添加到ViewModel中,并使用ToggleButton将其绑定。IsChecked依赖项属性:
public bool IsChecked {
get { return this.isChecked; }
set {
this.isChecked = value;
this.OnPropertyChanged("IsChecked");
}
}
<ToggleButton Content="AUD Reset" IsChecked="{Binding Path=IsChecked}" Height="23" HorizontalAlignment="Center" Margin="0" Name="button4" Command={Binding Path=ConnectCommand} VerticalAlignment="Center" Width="100" />
最后,在ViewModel的构造函数中初始化IsChecked属性:
public ViewModel() {
this.IsChecked = true;
}
public ViewModel() {
this.IsChecked = true;
}