Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

This is part 2 of a two posts series about the property IsSynchronizedWithCurrentItem. In the previous post, we saw what it does in Windows Presentation Foundation. In this post, we will see that this property is missing in Silverlight, and propose a way to simulate it.

Read the rest of this entry ยป

Print | posted on Wednesday, February 18, 2009 11:21 PM

Feedback

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Ed McPadden at 2/19/2009 6:12 AM Gravatar
Great post Laurent! Very clever approach.

I am very new to Silverlight (and WPF) so I found it very interesting to walk thru the attached property code.

One thing I did notice is that the twoway binding on the textbox does not always work. If I edit the text and navigate away from the browser or I click on the same item then the text changes in the listbox but if I edit the textbox and navigate to a different item in the listbox the text is not updated. I wasn't sure if this was somehow related to this approach or not.

I've seen another blog post that also attempts to solve the missing CollectionViewSource in a different way. You and your readers might be interested in looking at this as well:

http://blog.boschin.it/articles/Improving-Silverlight-2.0-databinding-with-a-CollectionViewSource-control.aspx

Thanks for taking the time to post this. As I am learning WPF and Silverlight I am wrestling with the missing functionality in Silverlight and really enjoy reading posts on getting around these limitations.

...Ed

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Boyan Mihaylov at 2/19/2009 10:45 AM Gravatar
It's better to create a custom control which inherits from ListBox, for example, and add the functionality in it. You won't have any memory leak problems.

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Laurent at 2/19/2009 11:23 AM Gravatar
Hi Boyan,

The definition of "better" is a matter of many things, including personal taste. For me, I try to stay clear of creating custom controls inheriting others. I find this approach annoying and generally speaking more work than using attached behaviors. If I decide to create a custom control inheriting ListBox, I also need one for ComboBox, one for TabControl, etc... On the other hand, my solution works with all controls deriving from Selector.

Another argument is that deriving a control will be less compatible with a WPF solution, which was my main motivation in this article. If IsSynchronizedWithCurrentItem is implemented in a future version of Silverlight, my solution will be easier to revert than if I create a collection of custom controls.

Finally, using attached behaviors allows me to create small granular "function modules" that I can add in a very targeted way to controls, and only if needed. A derived custom control, on the other hand, is a bigger block, and thus less easily maintainable and testable.

Again, it is greatly a matter of taste, and if you are fine with the limitations I list here, I wouldn't recommend absolutely against it. For myself, I really prefer attached behaviors to extend functionality.

Cheers,
Laurent

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Laurent at 2/19/2009 11:33 AM Gravatar
Hi Ed,

Thank you for your kind comment. I also considered implementing CollectionViewSource in Silverlight, but it seemed a bigger work (and less fun ;)) than using an attached behavior. Also, I really wanted to write an article about attached behaviors, and saw a good occasion here.

Cheers,
Laurent

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Josh Smith at 2/19/2009 1:54 PM Gravatar
Well done, Laurent. Thanks for sharing this. :)

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Miguel at 3/6/2009 11:35 AM Gravatar
This is a good example. I just have a couple of points to mention.

-If you subscribe to the event and your handler is in a static class, then you will avoid the memory leak.
-When you create a binding for an ObservableCollection, Silverlight doesn't use a weak link as expected.

I also prefer to use Attached Behaviours instead of UserControls, for the reason you mention. Another benefit is that you can't attach different behaviours, while multiple inheritance or creating a complex inheritance chains is a less maintanable alternative and sometimes not even an option. For example, we have some behaviours for Textboxes that will filter everything that's not a number. We could've created a NumbericTextbox. At that point it looks find, but we also have another behaviour that selects all the text in the textbox whenever we get the focus. Now we could create a AutomaticallySelectedTextBox (or something like that). The problem now is that sometimes we need to use both behaviours for the same instance. Someone might decide to continue by the inheritance path and instead of creating AutomaticallySelectedTextBox modify NumberTextBox. Now the name doesn't makes sense. So we name it AutomaticallySelectedNumericTextBox, which is now an obvious smell. I think I went to far, but the point is that there's room for both techniques and I find Attached Behaviours as a really nice feature that we should make part of our toolbox and learn when to choose it over inheritance.

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Laurent at 3/23/2009 3:18 PM Gravatar
These are great comments, Miguel. Thank you for taking the time to write them.

Cheers,
Laurent

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Miguel Madero at 3/24/2009 12:37 AM Gravatar
No problem, btw, Delay from the SL team recently posted a fix for the ItemsSource that now uses a weak event handler for the CollectionChanged event.

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by Laurent at 3/24/2009 9:26 AM Gravatar
I saw that too. David is a WPF Disciple and gave us a preview. Nice stuff.

# re: Simulating IsSynchronizedWithCurrentItem in Silverlight (part 2)

left by enochenoch at 5/1/2009 6:34 PM Gravatar
help me ! thank you.

pleace read the post:

http://silverlight.net/forums/t/89912.aspx

Comments have been closed on this topic.