views:

187

answers:

1

In my WPF project I have a custom control which is visually represented by a rectangle object. In XAML I put a number of rectangles based on this custom control. User should be able to select a group of these rectangles by mouse clicks and then do some actions with these selected rectangles.

How I should implement the possibility of selection: (1) should I simply add yet another custom property e.g. "Selected" and change it in MouseClick event handlers or (2) should I use native "Logical Focus" functionality?

+1  A: 

I'd argue that if you're building selection logic into your control, you may be duplicating functionality already provided by existing Selector subclasses such as ListBox.

Why not just host instances of your control inside a ListBox and let it do the selection logic?

HTH,
Kent

Kent Boogaart
Thanks Kent. Concerning visual layout - if I need, let's say , 20 horizontal rows of such rectangles, then should I use 20 ListBoxes?
rem
No, you would use one ListBox but change its ItemsPanel to be a Panel that meets your layout requirements. It uses a StackPanel by default, which is why each item is layed below the previous, but you can swap that out for another panel, or even write your own panel and stick it in there.
Kent Boogaart
OK, thank you.Please, yet another small question: I tried to build ListBox as you said and I see that the fact that item IsSelected visually shows by dark blue color of the ListBox itself but the rectangle in this Item of the ItemBox remains as it was. Is there a way to change this vice versa (color changed only on rectangle itself)? Where I should look for this?
rem