0

In a drawing application that allows user to add different objects like Rectangle, Ellipse, Text, Image etc, I have a "color box" that lets user see or change the fill of selected object(s). If there are multiple objects selected, I can't obviously show all, so I show the color of first selected object. When user changes the color of "color box", I want to change the fill of all selected objects. (That's also how VS properties window works btw)

How do I bind my "color box"'s Brush property (type Brush) to do this thing?

What I have already tried is this: I bind "color box"'s Brush property to selection object and then use a Converter that examines the selection object and if there is one or more objects in it, it returns the color of first selected object in Convert() function.

Problem however is that I have no way of implementing ConvertBack(), because it doesn't give me access to the selection object, so that I could iterate through the selected objects and assign them newly selected color.

Using MultiBinding is also of no use, as value parameter of ConvertBack() is a single object and doesn't give me access to the selection object. One idea was to supply selection object using ConverterParameter, but ConverterParameter is not bindable.

Another idea (hack?) was to keep a class level variable to point to selection object and assign in Convert() function (by passing selection object in MultiBinding) and then use that variable in ConvertBack(), but I don't know how safe it is and whether Convert() and ConvertBack() will always be called in order.

So how do I achieve this?

Community
  • 1
  • 1
dotNET
  • 33,414
  • 24
  • 162
  • 251

1 Answers1

0

OK. This turned out to be easier than I thought. For future readers, I simply added a property to my VM of Brush type (call it SelectionBrush) whose getter would return the Brush of first selected object, and whose setter would iterate through the entire selection and assign the Brush to appropriate objects (by checking their types). No more converters needed!

dotNET
  • 33,414
  • 24
  • 162
  • 251