tags:

views:

217

answers:

2

This button click method launches a Window called "(assemblyname).Reports" when a button with Content "Reports" is clicked:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Button button = (Button)e.OriginalSource;
    Type type = this.GetType();
    Assembly assembly = type.Assembly;
    Window window = (Window)assembly.CreateInstance(String.Format("{0}.{1}", type.Namespace, button.Content));
    window.ShowDialog();
}

But I want the Content attribute value of the button to be able to change, e.g. it might change to "Stock Reports" but I still want the clicking of the button to launch "(assemblyname).Reports".

Is there a way to add attributes to the button tag, e.g. "TheWindowFileName"?

<Button x:Name="btnReports" Content="Stock Reports" TheWindowFileName="Reports"/>

If not, how else can I add additional information to my button elements which I can read and process in code behind?

+5  A: 

Certainly you can use attached properties to add extra attributes to XAML elements, but for what you need you could probably just use the existing Tag property:

<Button x:Name="btnReports" Content="Stock Reports" Tag="Reports"/>
Matt Hamilton
very interesting, works great for what I needed, is it possible to add more than one of these? this MSDN page isn't that clear on it: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tag.aspx
Edward Tanguay
No, Tag is a property just like Width or Content - it can only be used once. To add your own, follow the attached properties link.
Matt Hamilton
A: 

Using Attached Property here can be an overkill but instead you can try to encapsulate your button behavior in a Command and pass the data you want to use in the command as a CommandParameter. That should do the trick.

idursun