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?