简体   繁体   English

WPF / C#上的名称比较是什么意思?

[英]What does name comparison mean on WPF/C#?

I have a textbox in my xaml --> x:Name="textBoxId" . 我的xaml中有一个文本框-> x:Name="textBoxId" In the code behind I have used a method OnRoutedEvent(object sender, RoutedEventArgs e) . 在后面的代码中,我使用了OnRoutedEvent(object sender, RoutedEventArgs e)

I also have used the following: 我还使用了以下内容:

source == textBoxId

How can I use name comparison instead of object comparison? 如何使用名称比较而不是对象比较?

What does name comparison mean? 名称比较是什么意思?

How can I use name comparison instead of object comparison? 如何使用名称比较而不是对象比较?

You need to cast the source to a FrameworkElement : 您需要将源转换为FrameworkElement

private void OnRoutedEvent(object sender, RoutedEventArgs e)
{
    FrameworkElement fe = sender as FrameworkElement;
    if (fe != null && fe.Name == "textBoxId")
    {
        //...
    }
}

There is no real point in doing this though. 但是这样做没有真正意义。 You might as well compare the references: 您最好比较一下引用:

private void OnRoutedEvent(object sender, RoutedEventArgs e)
{
    if(sender == textBoxId)
        ...
}

What does name comparison mean? 名称比较是什么意思?

I guess it means that you are comparing the names of two elements, rather than comparing the element references themselves. 我猜这意味着您正在比较两个元素的名称 ,而不是比较元素引用本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM