Derik Whittaker

Syndication

News


GridView SelectedItem Binding Gotcha in WinRT

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,


Posted 04-10-2012 3:32 AM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Michael wrote re: GridView SelectedItem Binding Gotcha in WinRT
on 04-27-2012 10:25 AM

Instead of binding to SelectedItem, you could add an event handler for the SelectionChanged event.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)