1

Is it possible to dynamically get the content or name from a label in WPF using C#?

I have a few labels like:

<Label x:Name="Server1" Content="Server1" MouseLeftButtonUp="ServerNameLabel_Click"/>
<Label x:Name="Server2" Content="Server2" MouseLeftButtonUp="ServerNameLabel_Click"/>
<Label x:Name="Server3" Content="Server3" MouseLeftButtonUp="ServerNameLabel_Click"/>

Is it possible to get the content or name ("Server1", "Server2", "Server3") using the same event handler like below?

private void ServerNameLabel_Click(object sender, RoutedEventArgs e)
{
    // Can I here get the content or name text from each label, based on the one I clicked?
}

Or do I need a unique event handler for each label?

-=Solution=-

private void ServerNameLabel_Click(object sender, RoutedEventArgs e)
{
    var label = sender as Label; // <--- by using sender object
    var content = label.Content;

    // Do stuff here...
}

Thanks for the duplicate information.

Qxlkdr
  • 23
  • 4

0 Answers0