Hide / Show TextBox when the value of another TextBox changes

0

I am developing an application in WPF and I came across a small problem.

I want that when the value of a TextBox is changed, other TextBox disappears, depending on the value of TextBox that is changing, I am using the pattern MVVM and I already tried with the property ObservableCollection<T> but you are not updating the data in ViewModel .

    
asked by Pistche Lawliet 02.07.2017 в 04:42
source

1 answer

1

There are several ways to achieve this, I will show you one:

First your ViewModel must implement INotifyPropertyChanged to communicate the changes to the user interface, otherwise they will not be reflected.

public MyViewModel : INotifyPropertyChanged
{
 //...
}

Now add these 2 lines to help you launch this event:

protected void RaisePropertyChanged(string propertyName)
{
     var handler = PropertyChanged;
     if (handler != null)
     {
        handler(this, new PropertyChangedEventArgs(propertyName));
     }
}

public event PropertyChangedEventHandler PropertyChanged;

Now, at " TextBox1 " make your property "Text" binding to a property of type string in the ViewModel,

<TextBox x:Name="TextBox1" Text="{Binding TextBox1Text, UpdateSourceTrigger="PropertyChanged"}"/>

and a TextBox " TextBox2 " bind your property Visibility to a property of type "Visibility" in the ViewModel

<TextBox x:Name="TextBox2" Visibility="{Binding TextBox2Visibility, UpdateSourceTrigger="PropertyChanged"}"/>

The properties in the ViewModel should look like this:

private string _textBox1Text;
public string TextBox1Text
{
    get { return _textBox1Text; }
    set 
    {
        _textBox1Text = value;
        RaisePropertyChanged("TextBox1Text");
    }
}

private Visibility _textBox2Visibility;
public Visibility TextBox2Visibility
{
     get { return _textBox2Visibility; }
     set
     { 
        _textBox2Visibility = value; 
        RaisePropertyChanged("TextBox2Visibility");
     }
}

Then in the string property that is binded to TextboX1 add after the set a line that is the name of a method, it should look like this:

private string _textBox1Text;
public string TextBox1Text
{
    get { return _textBox1Text; }
   set
   { 
      _textBox1Text = value;
      RaisePropertyChanged("TextBox1Text");
      VerificarValor(value);
   }
}

private void VerificarValor(string value)
{
  if(value.equals("clave"))
  {
    TextBox2Visibility = Visibility.Visible;
  }
  else 
  {
     TextBox2Visibility = Visibility.Hidden; //o collapsed
  }
}

And you follow the same logic to make the rest of the TextBox appear or disappear.

Another way would be to Bin the property Visibility in XAML of each TextBox to the property Text of the TextBox that will contain the keyword .. and then add in the binding the reference to a Converter that returns the value of Visibility according to the content you receive, in this case the Textbox text.

Although the Converters give you a more ordered and "standard" code, I prefer in this case to handle it directly in the ViewModel because it gives more control since you can manipulate the conditions at ease.

    
answered by 03.07.2017 / 03:43
source