views:

254

answers:

4

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. 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?

+2  A: 

I'm not awesomes at WPF but I believe you want to pass feSource into ChangeSelection instead. Then cast it to it's "real" type, be this CheckBox or whatever and then modify the .IsSelected property on it.

I have no idea why you would want to do this by name when you get the real object as the parameter (object sender).

Quibblesome
Just lack of C# experience prevents me from manipulating parameters more elegant. I need further learning :)
rem
Thank you! I have managed to do all this with real object, using casting to my type of object as you suggested. It's cool.
rem
+2  A: 

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.

Dave
I have to study this way a little. Just not very familiar with "tag" functionality. Thanks, anyway.
rem
+1  A: 

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?)

Where you declare r1s1 and r1s3 in your top function? 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;
}
antik
Special thanks for your syntax sugar comment. My variant looks ugly, now I see that :)
rem
+2  A: 

You want to access a control by its name. Following code assumes your control is a "RadioButton" and your code is on a form.

    public void ChangeSelection(string name)
    {
        if (this.Controls.ContainsKey(name))
        {
            RadioButton radio1 = this.Controls[name] as RadioButton;
            radio1.IsSelected = !radio1.IsSelected;
        }
    }
A9S6
Thanks for the direct answer to my newbie question. And thanks for code example, I needed it really. My problem with implementing this, that I can't clear identify what the "Controls" in my context means - it gives me constant error "..does not contain a definition for 'Controls'..."
rem
This is WinForm code whereas the OP is talking about WPF.
Quibblesome