Unable to disable controls (TextBoxes) using MVVM in WPF?

I have a text field, depending on the value of the text field. I need to enable / disable other text fields. I am using the MVVM pattern.

So here is my problem: whenever I enter some text in TextBox1, Setter for TextBox1 starts and I can check if there will be a loop, regardless of whether the valus value exists, and I will turn off the other text fields. Now that there is one value in the text field, say "9", and I delete / cancel it, the Set event does not fire to include other text fields.

View:

<TextBox Text = {Binding TextBox1 , UpdateSourceTrigger = PropertyChanged,Mode= TwoWay}/> <TextBox Text = {Binding TextBox2 , UpdateSourceTrigger = PropertyChanged,Mode= TwoWay}/> <TextBox Text = {Binding TextBox3 , UpdateSourceTrigger = PropertyChanged,Mode= TwoWay}/> 

Show model:

 private int_textBox1; public int TextBox1 { get {return _textBox1;} set { _textBox1= value; if(value > 0) { //Code for Disabling Other Text Boxes (TextBox2 and TextBox3) } else { // Code for Enabling Other Text Boxes (TextBox2 and TextBox3) } NotifyPropertyChanged("TextBox1"); } } 
+4
source share
2 answers

If you use the MVVM pattern, you must create boolean properties and bind the TextBox.IsEnabled property to it. Your boolean properties should raise a PropertyChanged event to tell you ( TextBox in your case) that your property has really changed:

 public bool IsEnabled1 { get { return _isEnabled1; } set { if (_isEnabled1 == value) { return; } _isEnabled1 = value; RaisePropertyChanged("IsEnabled1"); } } 

and then in xaml:

 <TextBox Text="{Binding TextBox1, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsEnabled="{Binding IsEnabled1}" /> 

etc. with other textboxes

+7
source

First of all, if you set your updateourcetrigger to Propertychanged - your setter is called when you do something in your text field. I test this in a simple test project. By the way, are you calling OnPropertyChanged in your setter because it is not in your sample code?

If your binding does not seem to work. so pls check your binding or post more suitable code.

EDIT:

if you change your type to int? you can do the following xaml

  <TextBox Text="{Binding MyNullableInt, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, TargetNullValue=''}"/> 
+1
source

Source: https://habr.com/ru/post/1484570/


All Articles