Question
What is Attached Properties and how to register it?

Answers

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:

<TextBox Width="200" Height="50" local:TextblockExtension.AllowOnlyString="True"></TextBox>   Your Comment




More Software Questions..
What is the inputsplit in map reduce software?

What is software configuration management?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?

What are the minimum system requirements to run Photoshop? Is it possible to run Photoshop over linux?

Which oracle package is used to manage the oracle lock management services?

What is Latch Up? Explain Latch Up with cross section of a CMOS Inverter. How do you avoid Latch Up?

What is marker interface?

What types of partitioning are there for BW?



Search
Can you Answer!!
  • Q Which family,order does the fish Pilot fish belongs to?
  • Q Critically discuss the impact of television on children.
  • Q Is it possible to describe in general terms (archetype) the race that once populated britain and already present when the celts arrived?
  • Q Test Oracle What is Test Oracle?
  • Q Who has been named to be the next US Ambassador to India by the US Senate?
  • Q Why ServiceMax? What do you know about ServiceMax? Company profile of ServiceMax?
  • Q Which computer works by processing the continuous signals such as heat, pressure, speed etc?
  • Q Which of the following Offices has not been provided in the Constitution?
  • Q Too many cooks _____ the broth.
  • Q Which of the following controls would BEST detect intrusion? A. User ids and user privileges are granted through authorized procedures. B. Automatic logoff is used when a workstation is inactive for a particular period of time. C. Automatic logoff of the system after a specified number of unsuccessful attempts. D. Unsuccessful logon attempts are monitored by the security administrator.
  • Q How many gold medals did Great Britain win at the 2008 Summer Olympics?