Solved

Button Control - read the Tag property

Posted on 2014-03-11
1
553 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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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