0

I'm trying to understand how a TextBox element/class in WPF can have the validation property e.g. in the code below that is in every tutorial I follow

    <Style TargetType="TextBox">
        <Setter Property="FontSize" Value="20">
        </Setter>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>

From what I understand, Trigger will track the value of Validation.HasError property of TextBox. However, I looked into the source code of TextBox and its parent classes, and nowhere there is a Validation property defined. So how does it exist in TextBox? I saw that TextBox and Validation classes are inside the same namespace System.Windows.Controls. Does that have anything to do with this?

And just to check my understanding, if I implement INotifyDataErrorInfo in a view model, then the Validation class of Microsoft (which I can't decompile) would internally track the ErrorsChanged event and set its HasError property to equal my implementation of HasErrors from the INotifyDataErrorInfo e.g. it would do something like this:

 public Validation(...){
              ...
              Errors += ErrorsChanged
              ...
         }
         private void Errors()
         {
              ...
              HasError = myImplementationOfINotifyDataErrorInfo.HasErrors
         }

or maybe

 public Validation(...){
              ...
         }
          public static readonly DependencyProperty HasErrorProperty =
        DependencyProperty.Register(
            name: "HasError",
            propertyType: typeof(bool),
            ownerType: typeof(Validation),
            typeMetadata: new FrameworkPropertyMetadata(null, ErrorsChanged));
         }

Am I right? For some reason I seem to can't open Validation class; I tried go to implementation in Visual Studio but it says it can't find the file.

EldHasp
  • 6,079
  • 2
  • 9
  • 24
aldo
  • 125
  • 1
  • 7
  • [Validation.HasError](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.validation.haserror?view=windowsdesktop-6.0) is an attached property. In Visual Studio you can always move the cursors on a type and then press F1 to navigate to the documentation of this class. To change the visual error feedback, you must define a corresponding DataTemplate. See [How to add validation to view model properties or how to implement INotifyDataErrorInfo](https://stackoverflow.com/a/56608064/3141792) to learn more. Validation is managed by the binding engine. – BionicCode Sep 17 '22 at 10:54
  • @BionicCode Yes I went to the docs but it's not documented there. Is there a list or a way to see attached properties on a class? – aldo Sep 17 '22 at 10:59
  • What exactly are you looking for? Validation is a static class that defines a set of attached properties. This properties are then read by the binding engine. Follow the above link to get a quick understanding on how property validation works. There no real logic in the Validation class. It just allows to set and get validation related values element specific (attached properties). The logic, the code that invokes those properties, is deep inside the binding engine of WPF. The binding engine will lookup a INotifyDataErrorInfo interface and subscribe to the ErrorsChanged event. Follow the link – BionicCode Sep 17 '22 at 11:04
  • "The logic, the code that invokes those properties is deep inside the binding engine of WPF" Yes I was looking for that, but it seems I won't be able to see that. I just want to make sure I totally understand everything and know what's possible. – aldo Sep 17 '22 at 11:06
  • INotifyDataErrorInfo works exactly like INotifyPropertyChanged. Binding engine will lookup the interface, subscribe to the event to track changes. You must take a look in the source code browser. But it won't help you to solve your problem on how to customize the visual error feedback as this is done via the API. For this you must consult the .NET API documentation as suggested before. The 2nd link that I've provided also gives you an idea about how the validation works and how to implement and use the interface properly. Based on your questions you should understand the API first. – BionicCode Sep 17 '22 at 11:10
  • Attached Property are created in a separate class from the object class they are attached to. That is why they are «Attached». The most common examples are Grid.Row/Column and Canvas.Top/Left/Botton/Right. Validation is a class containing several of these properties. See documentation https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.validation?view=windowsdesktop-6.0 – EldHasp Sep 17 '22 at 11:10
  • The 2nd link also shows how to customize the error feedback. – BionicCode Sep 17 '22 at 11:12
  • 1
    Or follow this link [Data validation (data binding overview)](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-6.0#data-validation) (it does give you useful information, but does not show how to implement the interface for property validation. It's focused on binding validation but it will show how to define the error template). – BionicCode Sep 17 '22 at 11:14
  • From Sharp's point of view, Attached Properties are DependencyProperty registered via the RegisterAttached method. Also, by convention, Set/GetNameProperty methods are added to the Attached Property to facilitate programming. There is no Set method for read-only properties. In Visual Studio, you can jump to their description by typing `Validation.GetHasError` or `Validation.HasErrorProperty` in some method's code and pressing F12. – EldHasp Sep 17 '22 at 11:20

0 Answers0