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.