简体   繁体   English

如何在C#中将对象名称作为参数传递给函数?

[英]How in C# to pass a name of the object as a parameter to function?

I have the code that change the state of boolean property on mouseclick, depending on name of the clicked object: 我有更改鼠标单击时布尔属性状态的代码,具体取决于所单击对象的名称:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement feSource = e.Source as FrameworkElement;
        switch (feSource.Name)
        {
            case "r1s1":
                if (r1s1.IsSelected == false)
                    r1s1.IsSelected = true;                 
                else
                    r1s1.IsSelected = false;
                break;
            case "r1s2":
                if (r1s2.IsSelected == false)
                    r1s2.IsSelected = true;
                else
                    r1s2.IsSelected = false;
                break;
            .............
        }
        e.Handled = true;
    }

I would like to do the same action passing the name of the sender (r1s1, r1s2,..and so on) as a parameter to function where this string combines with a name of property (IsSelected) just to optimize code. 我想执行相同的操作,将发送方的名称(r1s1,r1s2等)作为参数传递给该字符串,并与属性名称(IsSelected)组合以优化代码。 Something like that (just idea): 像这样(只是想法):

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement feSource = e.Source as FrameworkElement;

        ChangeSelection (feSource.Name)
    }


public void ChangeSelection(string name)
    {
        if (name.IsSelected == false)
            name.IsSelected = true;
        else
            name.IsSelected = false;
    }

Please, correct me. 请纠正我。 What I am doing wrong? 我做错了什么?

I'm not awesomes at WPF but I believe you want to pass feSource into ChangeSelection instead. 我对WPF并不满意,但我相信您想将feSource传递给ChangeSelection。 Then cast it to it's "real" type, be this CheckBox or whatever and then modify the .IsSelected property on it. 然后将其强制转换为“真实”类型,无论是CheckBox还是其他类型,然后修改其上的.IsSelected属性。

I have no idea why you would want to do this by name when you get the real object as the parameter (object sender). 我不知道为什么当您将真实对象作为参数(对象发送者)时,为什么要按名称进行此操作。

You might just want to store the target reference in the "tag" property of each item. 您可能只想将目标引用存储在每个项目的“标签”属性中。 Then you won't have all the magic strings to pass around. 这样一来,您将不会传递所有魔力。

You want to access a control by its name. 您想按其名称访问控件。 Following code assumes your control is a "RadioButton" and your code is on a form. 以下代码假定您的控件是“ RadioButton”,并且您的代码在窗体上。

    public void ChangeSelection(string name)
    {
        if (this.Controls.ContainsKey(name))
        {
            RadioButton radio1 = this.Controls[name] as RadioButton;
            radio1.IsSelected = !radio1.IsSelected;
        }
    }

What you're passing to your function is a string. 您要传递给函数的是一个字符串。 So when you're attempting to address name.IsSelected in your function, you're looking for the String.IsSelected method (does one exist?) 因此,当您尝试在函数中寻址name.IsSelected时,您正在寻找String.IsSelected方法(是否存在)?

Where you declare r1s1 and r1s3 in your top function? 在top函数中的哪里声明r1s1和r1s3? Those are the objects you should be trying to call .IsSelected on. 这些是您应该尝试调用的对象。

And a syntax sugar comment: 和语法糖注释:

public void ChangeSelection(string name)
{
   // resolve object from name here
   feObject.IsSelected = ! feObject.IsSelected;
}

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

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