Binding IsSelected Method in the ListView Control in WinRT

Basically, it’s not possible. However, this is the best (and only that works) workaround that I found.

Subclass the Listview

I tried everything. Absolutely everything.

I tried straightforward binding – that doesn’t work for IsSelected.
I tried using WinRT Xaml Toolkit – didn’t work.
I tried using Setters – they don’t work for WinRT.

Finally, I came across this solution; here’s the listview subclass (stolen directly from here:

    public class ListViewEx : ListView
        protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
            base.PrepareContainerForItemOverride(element, item);
            ListViewItem listItem = element as ListViewItem;
            Binding binding = new Binding();
            binding.Mode = BindingMode.TwoWay;
            binding.Source = item;
            binding.Path = new PropertyPath("IsSelected");
            listItem.SetBinding(ListViewItem.IsSelectedProperty, binding);

Obviously this does restrict the IsSelected property name. My class looks something like this:

    public class Person
        public string Name { get; set; }

I didn’t want to add an IsSelected property to this; because it doesn’t directly relate to the entity. My solution was to wrap this inside a class such as this:

    public class SelectableItem<T> : INotifyPropertyChanged
        private T _item;
        public T Item 
            get { return _item; }
                _item = value;

        private bool _isSelected;
        public bool IsSelected
            get { return _isSelected; }
                _isSelected = value;                

        public event PropertyChangedEventHandler PropertyChanged;
        protected void RaisePropertyChanged([CallerMemberName] string name = "")
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));

And build it like this:

                List<Person> population = GetPeople();
                SelectablePeople = new ObservableCollection<SelectableItem<Person>>(population
                    .Select(n => new SelectableItem<Person>() { Item = n, IsSelected = false }).ToList());

So, finally, bind the collection to the new subclassed listview like this:

        <controls:ListViewEx x:Name="ItemListView" Grid.Column="0" Grid.Row="1"
                        ItemsSource="{Binding SelectablePeople}"                        
                        Width="Auto" Height="Auto" >

                <DataTemplate x:Name="MyTemplate">
                    <StackPanel Orientation="Horizontal">
                            Text="{Binding Item.Name}"/>


1 thought on “Binding IsSelected Method in the ListView Control in WinRT

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s