Solved

Button Control - read the Tag property

Posted on 2014-03-11
1
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 23

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

617 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