WPF在单击复选框时传递多个参数

时间:2013-05-14 12:11:56

标签: wpf mvvm checkbox mouseevent

以下是我的xaml:

<CheckBox Name="CheckBoxNoFindings" Content="No Findings" Command="{Binding DisableRteCommand}" CommandParameter="{Binding Path=Content}" Grid.Row="1" Grid.Column="1" Margin="2,5,0,3" />

我想将IsCheckedContent属性值传递给命令参数并从VM访问它们。

VM代码:

private void DisableRte(object args)
{
    if (null != args)
    {
         string rteName = args.ToString();
    }
}

实际要求是,在check chekbox上,应禁用文本框,并将复选框的内容应用于texbox的文本。另一方面,取消选中复选框文本框应启用,文本应为空。

此方案的任何解决方案?

2 个答案:

答案 0 :(得分:2)

嗯,你希望它完成的方式,对我来说似乎有点奇怪。为什么不在VM中实现“简单方法”? E.g。

public class CheckBoxExampleVm : ViewModelBase //assuming you have such a base class
{
    private bool? _isChecked;
    public bool? IsChecked
    {
        get { return _isChecked; }
        set 
        {
            _isChecked = value;
            ModifyTextValue(value);
            RaisePropertyChanged("IsChecked");
        }
    }

    private string _textValue;
    public string TextValue
    {
        get { return _textValue; }
        set 
        {
            _textValue = value;
            RaisePropertyChanged("TextValue");
        }
    }

    private void ModifyTextValue(bool? condition)
    {
        // do what ever you want with the text value
    }
}

现在你只需要设置绑定,一切都很好。

另一种选择是使用转换器和元素绑定,这样您就不必在VM本身中实现它。

答案 1 :(得分:0)

如果其他建议不适合您,您可以将整个CheckBox传递给VM。

<CheckBox ... CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
相关问题