FlipView SelectedItem binding

For what ever reason, I’ve just lost some hours working around an issue with FlipView on Windows 10 ARM.
In the hope of saving you some time, lets take a walk through it together.
Consider this basic example:
  DataContext=”{Binding MyDataCollection}”
  ItemSource=”{Binding Items}”
  SelectedItem=”{Binding Current, mode=TwoWay}”>
Assuming you have an object (MyDataCollection) with an ObservableCollection (Items) and a Current property, you’d expect this to just work.  And you’d be right!
…Unless you’re on ARM.
On ARM it looks like the SelectedItem is ignored.  On closer inspection the FlipView is actually overwriting it.  First with the correct value, then with null, then again to the 0 item in the collection…
To work around this (I’m uncomfortable calling this a “fix”) you can set the SelectedItem binding in code behind after the loaded event fires which would otherwise screw everything up.
First, remove the SelectedItem Binding from your FlipView XML.
Now, add a Loaded event handler to the FlipView, and add the following code:
Binding b = new Binding();
b.Mode = BindingMode.TwoWay;
var p = new PropertyPath(“Current”);
b.Path = p;
fv_Items.SetBinding(FlipView.SelectedItemProperty, b);
This works because the FlipView is trying to set itself to the 0 item, regardless of what you say, and overwrites your property value in the process.
In this method, you are not setting the SelectedItem binding until after that tom-foolery is completed.

2 comments for “FlipView SelectedItem binding

  1. Richard Castle
    March 16, 2018 at 6:13 am

    Thank you for taking the time to document your findings here. After much head scratching we found this article and it matched the exact same problem we’ve been seeing. We’ve had a more challenging situation with two flipviews linked to common bindings, but your suggestions gave us a new way to solve the problem.

    • Rob
      April 2, 2018 at 10:43 am

      You’re welcome Richard,

      It really makes my day that I might have been able to help.

Comments are closed.