Texblock - Make a text-changing event

2

I have already seen that the TextBlock property does not have an event when the text changes. Is there any way to do an event when the text changes?

* Remembering that it is for WPF and not Windows Forms. This question is only for WPF.

    
asked by anonymous 18.12.2017 / 15:24

1 answer

2

Code-behind binding

Use a binding for the Text property and set it to DataContext of the screen. From here you can use NotifyOnTargetUpdated and TargetUpdated , like this:

<TextBlock Name="textBlock1" Text="{Binding MeuTexto, NotifyOnTargetUpdated=True}" 
           TargetUpdated="textBlock1_TextChanged"/>

There in the class use the new textBlock1_TextChanged method to handle.

Dependency Property

In the constructor of your class or in the Loaded event, add this. Font

DependencyPropertyDescriptor dp = DependencyPropertyDescriptor.FromProperty(TextBlock.TextProperty, typeof(TextBlock));

dp.AddValueChanged(textblock_principal, (object a, EventArgs b) =>
{
      // texto alterado!
});
18.12.2017 / 15:31