How can I create a variable which changes in function of the clicked item?

Viewed 59

I'm having an issue on a C# WPF code : I have multiple colored images on a window and when I click on it, it becomes blurred (this works). But my code is really bad optimized and I would like to get some help !

Here's a small part of my code :

private void BrownBlur(object sender, MouseButtonEventArgs e)
{
   Brown.BlurApply(blurRadius: 15, new TimeSpan(0, 0, 1), TimeSpan.Zero);
}

private void LightGreenBlur(object sender, MouseButtonEventArgs e)
{
   LightGreen.BlurApply(blurRadius: 15, new TimeSpan(0, 0, 1), TimeSpan.Zero);
}

And it goes like this for every color of the window...

I know that this is probably the worst way to code something, so I tried things like this :

private void isBlurred()
{
   this.BlurApply(blurRadius: 15, new TimeSpan(0, 0, 1), TimeSpan.Zero);
}

But this is the whole window that becomes blurred...

So I'm asking you guys if you know an alternative to "this.BlurApply" or if you know a way to create a variable which changes in function of the clicked item !

1 Answers

One event for all your senders

private void OnBlureObjClick(object sender, MouseButtonEventArgs e)
{ 
  UIElement obj = sender as UIElement; //other parent class or interface
  obj.BlurApply(blurRadius: 15, new TimeSpan(0, 0, 1), TimeSpan.Zero);
}

Then assign event to all your objets

Brand.OnMouseButtonClik +=OnBlureObjClick;
LightGreen.OnMouseButtonClik +=OnBlureObjClick;

Or create collection of your objects and than add event in foreach loop

foreach(var item in collection)
{
 item .OnMouseButtonClik +=OnBlureObjClick;
}
Related