0

Following is my 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" />

I want to pass both IsChecked and Content property values to command parameter and access those from the VM.

VM Code:

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

Actual requirement is, on check chekbox, a textbox should be disabled and content of checkbox should be applied into text of texbox. And opposite side , on uncheck checkbox textbox should be enabled and text should be empty.

Any Solution to this scenario?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Madurika Welivita
  • 890
  • 1
  • 10
  • 19
  • There are ways to do it, however it is still more common to have properties on your viewmodel to bind to instead. In your case, you could have 3 strings (your Contents) and 3 booleans (your IsChecked) Here's one example using multiple command params: http://stackoverflow.com/questions/1350598/passing-two-command-parameters-using-a-wpf-binding – Thelonias May 14 '13 at 12:36

2 Answers2

2

Hmm, the way you want it to be done, seems a bit odd to me. Why don't you implement it the "easy way" in your 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
    }
}

Now you only need to set the bindings and everything is fine.

Another option, would be using a converter and element binding, so that you don't have to implement it in the VM itself.

DHN
  • 4,807
  • 3
  • 31
  • 45
  • Here, the problem is I am having 03 checkboxes and 03 textboxes per checkbox, so I am trying to achieve this using one common command on all checkboxes.Thats why I need to pass content as well. – Madurika Welivita May 14 '13 at 12:30
  • Well then you could think about using a `ListView` so that you can bind to a collection of `CheckBoxExampleVm`. You can declare the composition in the item template then. The command could be implemented in the superior VM and work with the current `SelectedItem`. My suggestion would still work. - You could also consider creating a `UserControl` to reuse this composition. But I would prefer the first solution. – DHN May 14 '13 at 12:38
  • By thinking about it again. You wouldn't even need the command, if you would implement it with the `ListView`. The `CheckBoxExampleVm` would do it automatically. – DHN May 14 '13 at 12:45
1

You could pass the entire CheckBox through to the VM if the other suggestions don't work for you.

<CheckBox ... CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Murkaeus
  • 1,431
  • 1
  • 12
  • 12
  • 3
    Yes, a solution, but an ugly one. It totally breaks the MVVM pattern. **VM implementations should not contain any references to view related components such as controls or views**. – DHN May 14 '13 at 14:26
  • @DHN I totally agree, but it is sometimes necessary to implement quick 'n' dirty fixes, like not having time to refactor someone else's code. – Murkaeus May 14 '13 at 15:46
  • Hmm, only if you want to have a throw away result. ;o) If you're into real software development with its life cycle etc., then such solutions should definitely be avoided. But since we don't know the background a.s.o. it's all speculation. :o) – DHN May 14 '13 at 16:01
  • 1
    When you have all the time in the world it's easy to make something right. In real-world situations where time is money and there are bugs to fix, solutions aren't always pretty. – Murkaeus May 14 '13 at 16:26