Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into a gotcha that I thought I would share.
First, I was using the ‘Split Application’ template for my C# project. This is important because this template will create a bunch to boiler plate code for you and I was taking this code and extending it.
When I was setting up my SelectedItem binding had had the following Xaml
<GridView SelectedItem={Binding SelectedDashboardOption, Mode=TwoWay} …… />
When I ran the app everything ran fine but when I selected an item nothing happened. I quickly noticed what I thought was the issue, I had not enabled selection in the grid, aka SelectionMode=Single, so I added that. This made my Xaml look like:
<GridView SelectedItem={Binding SelectedDashboardOption, Mode=TwoWay} SelectionMode=”Single” …… />
I gave this a spin but nothing…..
Then I had a thought, maybe the bindings which allowed the Item to be clicked was causing this, turns out I was right. The boiler plate code does not use MVVM (a real shame and IMO this needs to be addressed by MS) but rather click events. This meant that the following attributes were set by default for the GridView via the template.
<GridView …… IsItemClickEnabled=”True” ItemClick=”ItemView_ItemClick” ….. />
All told my Xaml was both setup to handling SelectedItem binding AND the click event. Turns out this is not allowed and will not allow your binding to work.
To solve my issue I simply removed the IsItemClickEnabled and ItemClick attributes and the now I could get the binding for SelectedItem to work.
Till next time,
No comments:
Post a Comment
Thank's!