0

嘿,我刚开始研究切换按钮,我已经设置了按钮的属性。我想检查应该返回 1 或 0 的切换状态。

XAML:

<ToggleButton Grid.Column="3" Content="On" Command="{Binding VoltageToggleCommand}" IsChecked="{Binding Path=IsChecked}" Name="VoltageToggleBtn" />

视图模型类:

private ICommand mToggleButtonCommand;
    public ICommand VoltageToggleCommand
    {
        get
        {
            if (mToggleButtonCommand == null)
                mToggleButtonCommand = new DelegateCommand(new Action(ToggleButtonCommandExecuted), new Func<bool>(ToggleButtonCommandCanExecute));

            return mToggleButtonCommand;
        }
        set
        {
            mToggleButtonCommand = value;
        }
    }

    /// <summary>
    /// Check for ToggleButton State
    /// </summary>
    private bool isChecked;
    public bool IsChecked
    {
        get { return this.isChecked; }
        set
        {
            this.isChecked = value;
            this.OnPropertyChanged("IsChecked");
        }
    }

    public bool ToggleButtonCommandCanExecute()
    {
        return true;
    }

    public void ToggleButtonCommandExecuted()
    {
        Byte[] cmdArray = new Byte[256];
        int numBytes = 0;
        cmdArray[numBytes++] = // Here I wanna retrieve the toggle state i.e. 1 or 0
        // Some Code
    }

因此,当我单击切换按钮时,控件到达ToggleButtonCommandExecuted()我需要检查状态并将其存储在cmdArray. 基本上在执行代码时 cmdArray[numBytes++] = 1 或 0。

我怎样才能实现它?:)

4

2 回答 2

2

您必须自己将布尔值转换为字节

cmdArray[numBytes++] = (byte)(IsChecked ? 1 : 0);
于 2012-10-16T11:49:13.590 回答
1

使用if 短格式

IsChecked ? 1: 0
于 2012-10-16T11:46:46.323 回答