如何在代码中访问x:Name-property? [英] How to access x:Name-property in code?

查看:87
本文介绍了如何在代码中访问x:Name-property?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML文件中将x:Name分配给了一个可以触发MouseDown事件的对象.在这种情况下,我想再次获取发送者的x:name属性.我该怎么办?

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?

对象看起来像这样:

<ModelUIElement3D MouseDown="ModelUIElement3D_MouseDown" x:Name="trololo">

推荐答案

如果我正确理解了您的问题,则可以通过将发件人投射到 FrameworkElement 来访问 Name 属性.代码>.

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

或者,您可以只使用设计者创建的参考对象,实例名称与您在 x:Name 属性中指定的名称相同.

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)
    {
    }

  }

这篇关于如何在代码中访问x:Name-property?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆