问题
I need key and Value property in all controls which are drives from FrameworkElement class in wpf. key and value property are needed for some internal purpose. I know we have a Tag property which is used for saving custom data. I need two more such properties.
Any suggestions?
回答1:
You don't need to create another property, as you know Tag
property will allow you to save custom data.
This is example of how you can store data in the Tag
.
public class Customdata
{
public int Id { get; set; }
public int value { get; set; }
}
private void setDataInTag(FrameworkElement obj, Customdata objCustomData)
{
obj.Tag = objCustomData;
}
private Customdata GetValueFromElement(FrameworkElement obj)
{
Customdata objCustomData = new Customdata();
if (obj.Tag!=null && obj.Tag.GetType() == typeof(Customdata))
{
objCustomData = (Customdata)obj.Tag;
return objCustomData;
}
}
I think it is simple now :)
回答2:
You should create your own attached property.
Just create new class, write propa
codesnippet, press tab, tab :)
in xaml you can then set and get the property on any dependency object, just like you can use Grid.Column or Canvas.Left on any element.
来源:https://stackoverflow.com/questions/29228396/how-to-add-dependency-property-to-frameworkelement-driven-classes-in-wpf