Silverlight Data Binding bugs explained

by StefanOlson 30. October 2008 12:44

As I mentioned in my post Silverlight 2 RTW Bugs data binding is basically broken in Silverlight 2.  I wasn't at the time entirely sure exactly what the reason was, but I have now discovered it (or part of it).  It turns out that custom Dependency Properties, on which I rely for my data binding do not actually notify bindings of a change in state.  This is of course completely inconsistent with WPF and makes maintaining code between Silverlight in WPF even more difficult.  Hopefully the issue will be fixed in the next release of Silverlight.  My understanding is that the reason that was done this way was to keep the DependencyProperty system as small and fast as possible.  Unfortunately makes the programmer's life substantially more difficult!

So, how do we solve this problem?  This post Silverlight 2.0 - Bug with Data Binding? has a detailed description, including code, showing how to use INotifyPropertyChanged in your DependencyProperty changed handler.  As you can imagine with a large code base like we have for the Virtual Tour Viewer this is quite a massive amount of work and the original point of using Dependency Properties is that the change handling was automatic!

However, this solution proposed above will only work for situations where you can get a change notification.  There are a number of scenarios in Silverlight where there are simply no notifications.  So in many cases we are largely back at square one.  Mostly what I've had to do in the Virtual Tour Viewer is try and handle events and update my user interface that way.  This has of course resulted in an explosion of code for the Silverlight version of the tour and I have #if SILVERLIGHT all over the place.  Really you just have to be pragmatic about things otherwise you'd never actually get the job done.

So the best thing we can do at this point is to lobby Microsoft to make changes in the next version of Silverlight that will resolve these issues and bring consistency with WPF.

...Stefan

Tags:

Silverlight

Ribbon control to debut at PDC?

by StefanOlson 15. October 2008 12:37

From the description of this session coming up at PDC:

It looks like we'll be seeing the long awaited ribbon control!  You can check out all the PDC sessions and even watch them on streaming video 24 hours after the real event from this site: https://sessions.microsoftpdc.com/public/sessions.aspx.

It should be pretty exciting with WPF 4.0 and presumably Silverlight 2.5 being introduced for the first time.

Tags:

WPF

Silverlight 2 RTW bugs

by StefanOlson 14. October 2008 07:00

The Silverlight 2 release came out today.  The first thing I had to do was to revisit all the bugs that I reported during the release candidate period.  They were all reported via the Microsoft connect site, but very oddly they were closed by Microsoft with the reason being External.  Normally when I report a bug that is fixed they will be marked as fixed, so I assumed that they had not been fixed.  Unfortunately I was correct.

Update: it has been pointed out to me that by the time that I started filing bugs, which was five or six days after the release candidate was made available, Silverlight was basically in lock down and only major bugs would be fixed.  This explains why none of these issues were fixed obviously there was some concern about fixing issues in that timeframe that could have caused additional problems.  It is disappointing however that there were only five or six days before everything was locked down.  I was unable to do much development with beta 2 because there were simply too many issues, so I really had to wait for the release candidate.

So to help those of you who are developing Silverlight applications and don't want to waste the amount of time that I have trying to figure out whether these are bugs or not, here are the 7 bugs that I reported, and workarounds where possible:

DataContext = this

Setting the DataContext equal to this causes Internet Explorer to crash!

Workaround:  Rather than using this, you need to create a separate class to contain the items the you wish to be accessible from the data context.  I use DataContext = this; all over the place in the virtual tour software and it is a huge effort to remove that and create separate classes because quite often there are interrelated bindings which makes separating the classes very difficult.

Connect issue.

Converter not called after first time

I suspect this issue may be related to some of the other data binding problems. I haven't spent enough time looking in detail at all of them to be able to track down the exact details of the problems.  I have simply come to the conclusion that it is quicker to avoid using data binding if there are problems.

Using a converter to convert from a boolen into visibility the converter is called the first time but when the value changes thereafter the converter is not called even though the dependency property's value changed has been called.

Workaround: in this case, you would need to manually set the visibility rather than using the binding.

Update: this bug is simply the fact that DependencyProperties don't notify the binding unless you implement INotifyPropertyChanged.  See Silverlight Data Binding bugs explained for details.

Connect issue.

ScrollIntoView does nothing on a listbox

In the Virtual Tour Viewer, when the user types into the lookup field I scroll to the closest possible item.  You can see how this works below: 

 

Unfortunately, in WPF it doesn't actually appear at the top as I have shown it. It actually appears at the bottom if it is scrolling up.  If it is scrolling it down then it will appear at the top.  I have logged a feature request in WPF that there be an option for it to automatically go to the top.

However either way is better than Silverlight.  In Silverlight absolutely nothing happens! 

Update: after digging through the source code via reflector, what I discovered was that if you did not have a scrollviewer named "ScrollViewer" then nothing happens.  Once I renamed my scrollviewer everything worked! Hopefully they will make this clear in the documentation for future releases!

Connect issue

Can't reuse a tooltip in the same way as WPF

The following xaml will cause an exception when you attempt to display the tooltip:

<Grid.Resources>
   <ToolTip x:Key="TT">
      <TextBlock Text="Hello!"/>
   </ToolTip>
</Grid.Resources>
<Button ToolTipService.ToolTip="{StaticResource TT}" Content="button"/>

The code works perfectly in WPF but is just one of many problems with tooltips and Silverlight, and even bigger problem being that the data context of the tooltip is not set.  That means that you can't use binding within the tooltip.  The situation is so bad that in this blog: http://blogs.msdn.com/delay/archive/2008/03/12/lb-sv-why-three-wacky-uses-for-silverlight-2-s-listbox-and-scrollviewer.aspx the work around is to manually create the tooltip in code!  There are a couple of workarounds for the data context issue but they don't appear to work for me at this point, I shall do some more testing.

Workaround: manually create the tooltip in code

Connect issue

Data binding never updates child objects when parent changed

In a number of places in the virtual tour we reference a child, e.g: {Binding SelectedItem.Name}.  When SelectedItem changes, the name will be updated in the UI.  You can see an example here, where the title is the same as the tab name:

However in Silverlight when the selected item changes the data binding is not updated. 

e.g:

Binding b = new Binding("SelectedItem.Name");
b.Source = _descTabs;
BindingOperations.SetBinding(_Title, TextBlock.TextProperty, b);

BindingOperations is a class I have written to be compatible with WPF.  It will be released as part of a library of WPF compatibility functionality in the near future.

This is extremely annoying!

Workaround: Manually update the item in question when the referenced object changes.  This is not quite as easy as it sounds because even if you set up a new DependencyProperty, the changed handler quite often never gets called.  I guess it should be filed that as a separate bug but I was hoping that in the final release of Silverlight at least some of the data binding issues would be resolved and that would make it easier to then isolate any further problems.

Update: this bug is simply the fact that DependencyProperties don't notify the binding unless you implement INotifyPropertyChanged.  See Silverlight Data Binding bugs explained for details.

Connect Issue

Binding to a list and then changing the list fails

This XAML:

<Polygon Points={Binding MyPoints}/>

Then later call:

_dc.Points = new PointCollection();

Doesn't work in Silverlight.  When I put the new point collection into place, Silverlight does not update the polygon.

Workaround: Don't replace the whole collection, just call Clear() on the collection

Update: this bug is simply the fact that DependencyProperties don't notify the binding unless you implement INotifyPropertyChanged.  See Silverlight Data Binding bugs explained for details.

Connect issue

Assigning a point collection to two polygons causes exception

If you assign the same point collection to more than one polygon you will cause an exception (Value does not fall within the expected range).

Workaround: create two separate collections

Connect Issue


So that's it for all the bugs that I reported in Silverlight prior to release.  In the end I gave up on reporting bugs because they kept getting closed on connect.  Development in Silverlight is much more difficult with these bugs.  The biggest concern is data binding which just seems completely and utterly broken in all but the simplest cases.  I had hoped to replace most of my relative source and element name bindings from WPF with the manual bindings created in code.  Unfortunately maybe 75 percent of the time these manual bindings don't work at all!

I hope Microsoft can do better with their next release.  My understanding as this will be released in the near future, most likely called 2.5.  I would therefore expect that it will be demonstrated at PDC in a couple of weeks time.  I hope they focus on improving compatibility with WPF, which at this point is extremely disappointing.

Tags:

Silverlight

Silverlight goes to RTW

by StefanOlson 14. October 2008 00:00

Silverlight version 2 has been released today!  If you've been stuck on a desert island somewhere over the past two years you won't know that Silverlight is a version of the .net framework designed to run in your web browser.  It utilises a cut down version of the WPF XAML specification to provide the user interface.

I have been building both a WPF and Silverlight version of the virtual tour software we produce (www.palacevirtualtours.com).  Despite Microsoft claiming that Silverlight is a cut down version of WPF, the differences between the two systems are large enough that it takes a substantial amount of time to produce a version that runs under both systems.

During the Silverlight release candidate I reported 7 bugs on the Microsoft connect site.  So this morning's effort is to go through and see how many, if any were fixed.  My first look doesn't look encouraging, but we'll see how we go!

...Stefan

 

Tags:

Silverlight

About the author

Stefan Olson is the Managing Director of Olson Software.  He has been developing software using Microsoft Technologies for nearly 20 years.

He is currently working on building the next generation Virtual Tour software in WPF and Silverlight for www.palacevirtualtours.com.

Tag cloud