0

There is a TextBox on window whose value is binded to view model property (simple, usual binding).

<TextBox x:Name="textBoxName" Text="{Binding Name}"/>

Now I need to show on the TextBox either the value coming from binding or empty string depending on RadioButton value on the same window (and view model).

One idea to achieve the goal is to programmatically clear the binding, set empty value, and later set the binding again. But I guess this is not a good solution.

I'm new to WPF and MVVM and would like to hear how this should be done properly (in "MVVM way")?

joof
  • 23
  • 7
  • You can use a converter – mshwf Apr 17 '18 at 09:00
  • How about using a MultiBinding? Alternatively a DataTrigger on the RadioButton IsChecked property (or an appropriate view model property) that sets the Binding. – Clemens Apr 17 '18 at 09:06
  • This is a textbox? Not a textblock? Seems a bit strange to me. What happens when the user has checked the radio button and types into the textbox that is kind of bound to Name but maybe isn't. Is it supposed to still update name or what? – Andy Apr 17 '18 at 09:31
  • @Andy When the TextBox value is cleared, The TextBox is also disabled (which part is already working). – joof Apr 17 '18 at 09:54
  • You could maybe just collapse it instead of setting isenabled false. – Andy Apr 17 '18 at 09:56
  • @Clemens Can MultiBinding be used for this? According to [this Q](https://stackoverflow.com/questions/2552853/how-to-bind-multiple-values-to-a-single-wpf-textblock) it can be used to combine two values. But can it be used to select one from two? – joof Apr 17 '18 at 09:57
  • You can put code in a multiconverter on a multibinding. – Andy Apr 17 '18 at 10:00

2 Answers2

0

There are a few things you can do here.

  1. Change value of Name Property based on what was selected on Radio Button

  2. Use a converter that checks radio button value and updates text box text

  3. Use Trigger to change the text box text when radio button selection is changed.

All three are fairly easy to implement, let me know if you need code for any.

Muds
  • 4,006
  • 5
  • 31
  • 53
0

You could use a multi value converter:

public class BooleanMultiValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            bool isChecked = (bool)values[0];
            string name = (string)values[1];
            return isChecked ? "" : name;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

then you bind your TextBox like this:

<StackPanel>
        <TextBox>
            <TextBox.Text>
                <MultiBinding Converter="{StaticResource MultiValueConverter}" >
                    <Binding Path="IsChecked" ElementName="rb"/>
                    <Binding Path="Name"/>
                </MultiBinding>
            </TextBox.Text>
        </TextBox>
        <StackPanel Orientation="Horizontal">
            <RadioButton Name="rb"/>
            <RadioButton/>
        </StackPanel>
    </StackPanel>

add this as direct child to your window:

<Window.Resources>
    <converters:BooleanMultiValueConverter x:Key="MultiValueConverter"/>
</Window.Resources>

and don't forget to add the namespace:

xmlns:converters="clr-namespace:WPFTest.Converters"

This should work:

enter image description here

mshwf
  • 7,009
  • 12
  • 59
  • 133