How to avoid Converters clashing with multibinding in WPF code behind How to avoid Converters clashing with multibinding in WPF code behind wpf wpf

How to avoid Converters clashing with multibinding in WPF code behind


It's relatively easy. Everything should resolve around CheckBox IsChecked property. For a simple reason, it's a two-way property. So either you can modify it, or CheckBox can modify it.

So what you do, you use MultiBinding, as such:

    MultiBinding multiBinding = new MultiBinding();    multiBinding.Converter = multiBindingConverter;    multiBinding.Bindings.Add(new Binding("Text") { Source = txtbox1});    multiBinding.Bindings.Add(new Binding("Text") { Source = txtbox2});    multiBinding.NotifyOnSourceUpdated = true;//this is important.     checkBox.SetBinding(CheckBox.IsCheckedProperty, multiBinding);

And in your multiBindingConverter, you will have object[] value as first parameter, which you need to convert into IList and iterate over it && do your calculations, if you should either return true/false.(IsChecked=true or false)

Now bind CheckBox IsEnabled to CheckBox IsChecked property, and use BooleanInverterConverter. (If CheckBox is checked, it should be disabled, and vice versa)

The last step is to make TextBoxes listen to actual IsChecked property of CheckBox.If it is TRUE, they all should show value of 0, otherwise they can show what they want.

So, make a new MultiBinding.

    MultiBinding multiBinding = new MultiBinding();    multiBinding.Converter = textboxMultiBindingConverter;    multiBinding.Bindings.Add(new Binding("IsChecked") { Source = checkbox1});    multiBinding.Bindings.Add(new Binding("Text") { Source = textbox1});    multiBinding.NotifyOnSourceUpdated = true;//this is important.     textbox1.SetBinding(TextBox.Text, multiBinding);

the idea in textboxMultiBindingConverter is to either return Text(value[1]) if value[0]==FALSE or "0" if value[0]==TRUE.


This problem can be solved very easily if you would use MVVM.

You would have a ViewModel that represents a row in the grid. It would have a property per textbox and one for the checkbox.

Additionally you would have a ViewModel for the View containing the Grid and this ViewModel would expose a collection of row ViewModels.

The ViewModel for your row:

public class AnswersViewModel : ViewModelBase // From MvvmLight{    public bool IsAnswered    {        get { return _isAnswered; }        set        {            if(value == _isAnswered)                return;            _isAnswered = value;            if(_isAnswered)            {                Answer1 = "0";                Answer2 = "0";            }            RaisePropertyChanged("IsAnswered");        }    }    public string Answer1    {        get { return _answer1; }        set        {            if(value == _answer1)                return;            _answer1 = value;            RaisePropertyChanged("Answer1");            if(_answer1 == "0" && _answer2 == "0")            {                _isAnswered = true;                RaisePropertyChanged("IsAnswered");            }        }    }    // The implementation of Answer2 is similar to Answer1}

The ViewModel for the View:

public class FooViewModel : ViewModelBase{    public ObservableCollection<AnswersViewModel> Answers    {        get { return _answers; }    }}

Your View would contain the Grid with ItemsSource="{Binding Answers}" and a ControlTemplate for the items which binds to the properties of AnswersViewModel.

Disabling the CheckBox I would handle via a Trigger in a Style.