1

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?

107
  • 552
  • 3
  • 26

2 Answers2

0

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 :)

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
  • Tag property is already used to store another piece of data. Don't want to have token separated value as it will cost at receiving end while splitting the token separated value. – 107 Mar 24 '15 at 09:30
-1

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.

Liero
  • 25,216
  • 29
  • 151
  • 297