An Interesting Way to Use The FrameWork Element, Tag

The other day I needed to send an object with a control for a dashboard type interface. I started messing around with the tag and realized in .Net 4.0 and WPF, it can hold an object, not just text. So let’s say I had an object ControlInfo. I then wanted to added that to the control using the tag, I would do something like the following.

private void SomeAddControlFunction(object sender)
{
   Control control = sender as Control;

   ControlInfo NewControlInfo = new ControlInfo();
   NewControlInfo.Name = "Test Name";
   NewControlInfo.Desc = "Test Desc";

   control.Tag = NewControlInfo;
}

private void SomeClickEventFunction(Object sender, RoutedEventArgs e)
{
   Control InControl = sender as Control;
   ControlInfo InControlInfo = InControl.Tag as ControlInfo;

   //Now we can use the object for whatever we need.
}

Jacob Saylor

Software developer in Kentucky

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: