Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Button Control - read the Tag property

Posted on 2014-03-11
1
Medium Priority
?
565 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 400 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

721 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