Solved

Button Control - read the Tag property

Posted on 2014-03-11
1
550 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
ID: 39920427
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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