views:

45

answers:

3

A WPF window should have hundreds of objects (rows of rectangles) and mouse clicking on each of them should fire similar actions. These actions differ only by several parameters (say "No. of Row" and "No. of position in a row"). Should I have hundreds of almost the same event handlers or how I could optimize my code?

Please give me some tips, just to move to the right direction.

Best regards.

A: 

I'm no expert in WPF, but in event handling you could write 1 dans point every similar event to this handler. In the handler use the senter parameter to know whish control it came from.

Instead of 100's of similar event hander you could have a big one with a switch

Hope that's help

Stéphane
Your answer was helpful. Thank you.
rem
A: 

WPF mitigates this problem by introducing Routed Events. At any level in the element hierarchy you may intercept events from its child elements and base your logic depending on the actual element that received this event in the first place (as presented by the Source property of RoutedEventArgs).

Pawel Marciniak
Thanks for giving right direction
rem
A: 

Can't you just use an instance of ICommand on your viewmodel and use the command parameter to determine which rectangle was clicked?

klausbyskov
ViewModel pattern is something that I really should dig into. I think I will appreciate your answer after some efforts in learning this way of doing things. Thank you for inspiring.
rem