无法将切换状态检索为0或1

时间:2012-10-16 11:44:33

标签: c# .net wpf mvvm toggle

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

XAML:

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

ViewModel类:

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。

我怎样才能实现它? :)

2 个答案:

答案 0 :(得分:2)

你必须自己从布尔转换为字节

cmdArray[numBytes++] = (byte)(IsChecked ? 1 : 0);

答案 1 :(得分:1)

使用if short form

IsChecked ? 1: 0