Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add additional attributes to standard XAML elements?

Tags:

wpf

xaml

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?

like image 689
Edward Tanguay Avatar asked Feb 20 '09 08:02

Edward Tanguay


Video Answer


1 Answers

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"/>
like image 136
Matt Hamilton Avatar answered Nov 15 '22 09:11

Matt Hamilton