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?

1
  • 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: stackoverflow.com/questions/1350598/… Commented May 14, 2013 at 12:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

4 Comments

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 I totally agree, but it is sometimes necessary to implement quick 'n' dirty fixes, like not having time to refactor someone else's code.
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)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.