Solved

Button Control - read the Tag property

Posted on 2014-03-11
1
545 Views
Last Modified: 2014-03-11
I came across some code in a book I'm reading and decided to give it a try.  I'm trying to read the tag property of a button when its clicked.

<Window x:Class="HandlManyButtonsOneEventEx.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Button.Click="DoSomething">
        <Button Name="cmd1" Tag="Button 1">Command 1</Button>
        <Button Name="cmd2" Tag="Button 2">Command 2</Button>
        <Button Name="cmd3" Tag="Button 3">Command 3</Button>        
    </StackPanel>
</Window>

Open in new window


public void DoSomething(object sender, RoutedEventArgs e) 
        {  
            object tag = ((FrameworkElement)sender).Tag;
            MessageBox.Show((string)tag);
        }

Open in new window


Every time i click the button, the tag returns a null value.  Is this code correct?  What am I doing wrong?
0
Comment
Question by:mmsi
1 Comment
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 100 total points
Comment Utility
It's almost right.  Even though the event is targeting the buttons, since it's declared on the StackPanel, THAT's what's being passed in as the sender.
Use the .Source property of the routedeventargs instead.

public void DoSomething(object sender, RoutedEventArgs e)
{
    object tag = ((FrameworkElement)e.Source).Tag;
    MessageBox.Show((string)tag);
} 

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now