Add attributes similar to AttachedProperty for common objects. objectproperty

Source: Internet
Author: User

Add attributes similar to AttachedProperty for common objects. objectproperty

Add attributes similar to AttachedProperty for common objects

Zhou yinhui

 

We know that in WPF, A DependencyObject corresponds to, and we can easily append an attribute for the Type through AttachedProperty. However, this is not feasible for common objects.

I have encountered such a problem. Below is a common class that inherits from objects (instead of DependencyObject:

    public class People    {        public string Name        {            get;            set;        }        public People(string name)        {            Name = name;        }        public override string ToString()        {            return Name;        }    }}

 

I need it to have the IsKeyPerson attribute in some cases. When this attribute is true, the interface will mark the name in red. But I cannot modify the implementation of this People. It would be nice if it was DependencyObject, but unfortunately it was not.

My display of this People class interface will probably look like this:

<DataTemplate DataType = "customAttachedProperty: People"> <TextBlock x: Name = "TextBlock" Text = "{Binding Name}"/> <DataTemplate. triggers> <DataTrigger if IsKeyPerson is true> <Setter TargetName = "TextBlock" Property = "Foreground" Value = "Red"/> </DataTrigger> </DataTemplate. triggers> </DataTemplate>

 

Specific how to achieve, download code: http://files.cnblogs.com/zhouyinhui/CustomAttachedPropertySln.rar

 

 

 


To put it simply, the similarities and differences between dependency attributes and common attributes in wpf

Here is a detailed introduction:
In fact, the dependency attribute is an attribute stored outside the instance. It provides a series of functions through metadata descriptions of some attributes. The term "Dependency" is initially derived from "Dependency Injection", that is, the attribute is not an instance, but an external Injection. It is possible that the "Attached Dependency Property" can better illustrate the problem and append an attribute that is not available to an object.

Attributes of the object in AS3

Custom Attributes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.