in Education by
What is Attached Properties and how to register it?

1 Answer

0 votes
by
Attached Properties (AP) 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. One of the misconceptions that a WPF developer usually has is that these properties can only be defined in the parent control the control in which we want to use them.
 
AP can be defined in one of the following three contexts:
  • 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.
Example
  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
0 votes
    What is WPF Dependency Property and how can we use?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
    How can I clip or crop an image?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
0 votes
    What is the difference between Static and Dynamic resources?...
asked Apr 9, 2021 in Education by JackTerrance
0 votes
0 votes
    What is the Tab Control in WPF?...
asked Apr 9, 2021 in Education by JackTerrance
...