Recently i've read 'Databinding overview' article at MSDN and there is such sample code:
<TextBox.ToolTip>
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)[0].ErrorContent"/>
</TextBox.ToolTip>
I know that {}
means markup extensions but what mean ()
parentheses here? It would be nice someone share link to explanation such syntax. Thanks!
Path="(Validation.Errors)[0].ErrorContent"
Binding paths address the different properties and lists in a model and define how a node in the hierarchical data tree can be found. A binding path consists of a number of name tokens, which are separated by a separator char. In all models provided by the framework, the separator char is the slash "/".
Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
The DataContext property is the default source of your bindings, unless you specifically declare another source, like we did in the previous chapter with the ElementName property. It's defined on the FrameworkElement class, which most UI controls, including the WPF Window, inherits from.
The ()
parentheses refer to Attached Properties.
Binding to an Attached Property
Quoting the MSDN library (I'm quoting MSDN here because I couldn't have written it down better):
This syntax is generally used for one of the following cases:
- The path is specified in XAML that is in a style or template that does not have a specified
TargetType
. A qualified usage is generally not valid for cases other than this, because in non-style, non-template cases, the property exists on an instance, not a type.- The property is an attached property.
- You are binding to a static property.
For use as storyboard target, the property specified as
propertyName
must be aDependencyProperty
.
(Validation.Errors)
references the attached property Errors
in the Validation
class. Since the binding has a RelativeSource = Self
, it's gonna look for the value of that attached property with respect to the TextBox itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With