tags:

views:

22

answers:

1

I assigned x:Name in my XAML-file to a object which can trigger a MouseDown-event. In that event I'd like to get the x:name-attribute of the sender again. How do I do that?

The object looks like that:

<ModelUIElement3D MouseDown="ModelUIElement3D_MouseDown" x:Name="trololo">
+3  A: 

If I have understood your question correctly, you can access the Name property by casting the sender to a FrameworkElement.

Alternatively you can just use the reference object that is created by the designer, the instance name is the same as the name that you specify in the x:Name attribute.

The following demonstrates both options.

  private void ModelUIElement3D_MouseDown(object sender, MouseButtonEventArgs e)
  {
    var element = sender as FrameworkElement;

    if (element != null)
    {
      if (element.Name == "trololo")
      {
      }
    }

    // Or

    if (sender == trololo)
    {
    }

  }
Chris Taylor
Casting it to a FrameworkElement and then accessing doesn't seem to work but sender == trololo does.Thank you!
Hedge
@Hedge, the reason the cast to FrameWorkElement does not work is because the ModelUIElement3D inheritance chain does not derive from FrameworkElement. I had to go check that, but I am glad you have solution.
Chris Taylor