in Education by
What is Attached Properties in WPF ?

1 Answer

0 votes
by

Attached properties are basically Dependency Properties that allows the attachment of a value to any random object. Attached Properties (AP) are again a kind of Dependency Property (DP) in XAML. They can be used to receive a notification of a change of themself since they are a type of Dependency Property but one of the differences that these properties have is that they are not defined in the same class they used, unlike DPs.

  • The type that defines the Attached Property is designed so that it can be the parent element of the elements that will set values for the Attached Property. The type then iterates its child objects using internal logic against some object's tree structure, obtains the values and acts on those values in some manner.
     
  • The type that defines the Attached Property will be used as the child element for a variety of possible parent elements and content models.
     
  • The type that defines the Attached Property represents a service. Other types set values for the Attached Property. Then, when the element that set the property is evaluated in the context of the service, the Attached Property values are obtained using internal logic of the service class.
APs are called “attached” properties because we can attach some behaviour to the control that is originally not expected from that control.
 
Example
 
Suppose I have a TextBox control and I want to extend its functionality to accept letters and special symbols but not numeric values.
 
The AP that I have defined in my class TextBlockExtension.cs is as in the following:
  1. public static bool GetAllowOnlyString(DependencyObject obj)  
  2. {  
  3.     return (bool) obj.GetValue(AllowOnlyStringProperty);  
  4. }  
  5. public static void SetAllowOnlyString(DependencyObject obj, bool value)  
  6. {  
  7.     obj.SetValue(AllowOnlyStringProperty, value);  
  8. }  
  9. // Using a DependencyProperty as the backing store for AllowOnlyString. This enables animation, styling, binding, etc...   
  10. public static readonly DependencyProperty AllowOnlyStringProperty = DependencyProperty.RegisterAttached("AllowOnlyString"typeof (bool), typeof (TextblockExtension), new PropertyMetadata(false, AllowOnlyString));  
  11. private static void AllowOnlyString(DependencyObject d, DependencyPropertyChangedEventArgs e)  
  12. {  
  13.     if (d is TextBox)  
  14.     {  
  15.         TextBox txtObj = (TextBox) d;  
  16.         txtObj.TextChanged += (s, arg) =>  
  17.         {  
  18.             TextBox txt = s as TextBox;  
  19.             if (!Regex.IsMatch(txt.Text, "^[a-zA-Z]*$"))  
  20.             {  
  21.                 txtObj.BorderBrush = Brushes.Red;  
  22.                 MessageBox.Show("Only letter allowed!");  
  23.             }  
  24.         };  
  25.     }  
  26. }  
In the code, as we can see, the AP has the default value of False, in other words we need to provide the APs the value true wherever I want this functionality to work for the TextBox.
 
In my MainWindow.xaml.cs I have defined my TextBox as in the following:
  1. <TextBox Width="200" Height="50" local:TextblockExtension.AllowOnlyString="True"></TextBox> 

Related questions

0 votes
    What is Attached Properties and how to register it?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is the Tab Control in WPF?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What is the Control Template in WPF?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    What is WPF Dependency Property and how can we use?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
    What is the Data Binding concept and How Binding works in WPF?...
asked Apr 8, 2021 in Education by JackTerrance
...