In the window there is a button whose IsEnabled property should be true only if the Text in the TextBox matches the Regex pattern. In other cases, false . You also need to follow the MVVM pattern.

View.xaml

 <Button Content="CLICK!" Height="45" IsEnabled="{Binding NameIsValid, Mode=OneWay}" Width="127" /> <TextBox Height="32" Text="{Binding Name, Mode=OneWayToSource}" FontSize="20" Width="120"/> 

ViewModel.cs

 public bool NameIsValid { get { return new Regex(@"^([AZ]|[А-Я]|І|Ї)([az]|[а-я]|і|ї)+").IsMatch(Name); } } public string Name { get => user.Name; set { user.Name = value; OnPropertyChanged("Name"); OnPropertyChanged("NameIsValid"); } } 

When a valid value is entered into the TextBox IsEnabled property of the button is not updated, but remains as it was at startup.

How to update the property of an element when changing another in my case?

PS

Found that the update happens only when the TextBox is out of focus. Is it possible to see updates immediately when typing?

  • Well, the getter itself is called? - tym32167
  • @ tym32167 Isn’t OneWayToSource doing this? - Aqua
  • I'm NameIsValid about the getter of this property NameIsValid , it is called after this OnPropertyChanged("NameIsValid"); ? - tym32167 9:10 pm
  • @ tym32167 a little I do not understand you. OnPropertyChanged ("NameIsValid") did in the Name property because I want to update the state of the button when the text changes. Made to update all subscribers. Doesn't that work? - Aqua
  • run your application, put a breakpoint here on this line return new Regex(@"^([AZ]|[А-Я]|І|Ї)([az]|[а-я]|і|ї)+").IsMatch(Name); , change the text in the textbox and tell me, did breakpoint work or not? - tym32167

1 answer 1

The problem was that the binding was updated when the focus was lost, which is the default behavior for the textbox. You need this

 <TextBox Text="{Binding Name, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" /> 

Update binding when the Textbox Text property changes.