Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在启动时设置按钮的ToggleState并检查状态_C#_Wpf_Mvvm_Togglebutton - Fatal编程技术网

C# 在启动时设置按钮的ToggleState并检查状态

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" /

我的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上,单击我要检查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;
}