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:
<FlipView
  Name=”fv_Items”
  DataContext=”{Binding MyDataCollection}”
  ItemSource=”{Binding Items}”
  SelectedItem=”{Binding Current, mode=TwoWay}”>
</FlipView>
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.
Cheers,
Rob.

2 comments for “FlipView SelectedItem binding

Leave a Reply

Your email address will not be published. Required fields are marked *