Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MVVM data binding broken after page navigation

I have a Page within a WPF navigation-style application that displays search results. The page contains several data-bound controls. The page itself works fine; it performs the search and returns results. The data-bound CheckBox controls work properly.

But if I click a result link and then click the back button to return to the results list, all of my CheckBox.IsChecked data bindings are broken. Other data bound controls (ComboBoxes, DatePickers, etc.) continue to work as expected. Binding to other properties on the CheckBox control, like IsEnabled, works properly. But the IsChecked binding remains broken until I refresh the page.

Here's the XAML used for one of my CheckBox controls:

<CheckBox IsChecked="{Binding IncludeNote}" Content="Note" IsEnabled="{Binding IsBusy, Converter={StaticResource boolNot}}" />

As you can see, there's nothing fancy going on here. But after navigating the WPF app forward or backward to the page, the IsChecked binding will be broken while the IsEnabled property will continue to work.

What's going on here? Is this a bug?

UPDATE: After playing around with some alternatives, I discovered that this problem also affects the ToggleButton control, which CheckBox derives from.

UPDATE2: And it's also broken for the TextBox.Text property.

Is there a way to "refresh" the data bindings for these controls? Or should I take some other approach to troubleshooting this issue?

like image 477
dthrasher Avatar asked Sep 02 '10 03:09

dthrasher


1 Answers

Apparently, it is a bug. Here's the bug report on Microsoft Connect: Binding does not work after back / forward navigation.

The user who reported the bug, RQDQ, also mentioned his approach to dealing with the problem:

The workaround I've found is to manually call BindingOperations.SetBinding for all bindings in the Page during the Loaded event. This appears to work whether navigating explicitly or via history (back / forward operations).

This is only an issue in WPF4. Data binding works as expected in .NET 3.5.

I hope Microsoft fixes this quickly. This is a serious problem for navigation-style WPF apps.

like image 101
dthrasher Avatar answered Sep 25 '22 23:09

dthrasher