How to add dependency property to FrameworkElement driven classes in WPF?

本秂侑毒 提交于 2019-12-12 03:14:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!