1

If I have a collection of items bound to my GridView, and each item has a property IsSelected, how can I make sure the state of that GridViewItem is selected?

I've tried binding the property in the ItemContainerStyle with no luck.

Am I better off using an ItemTemplateSelector you think? Maybe I'll try that while I wait for any feedback.

earthling
  • 5,084
  • 9
  • 46
  • 90

1 Answers1

2

This has always been a pain due to the lack of support for Value's binding in the Style's Setter in Winrt, but there is a workaround for that, which has been adapted to winrt (it originally targets the same limitation in Silverlight 4 - ps: Slverlight 5 support binding in Setters-), you can check it here,

but even so, for some reason this also doesn't work in Winrt :

<GridView SelectionMode="Multiple" HorizontalAlignment="Stretch">
    <GridView .ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="True"/>
        </Style>
    </GridView .ItemContainerStyle>

Now unless you find a better solution, here a little hack inspired from here that doesn't look so clean, but it do the trick

  • Extend the GridView class

    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    public class GridViewEx : GridView
    {
        protected override void    PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
       {
           base.PrepareContainerForItemOverride(element, item);
           var gridItem = element as GridViewItem;
           var binding = new Binding { Mode = BindingMode.TwoWay, Source =    item, Path = new PropertyPath("IsSelected") };
           gridItem.SetBinding(SelectorItem.IsSelectedProperty, binding);
       }
    }
    
  • Make sure that IsSelected property is present in your GridView ItemSource Collection

    public class Item
    {
       public String Name { get; set; }
       public bool IsSelected { get; set; }         
    }
    // ..
    public ObservableCollection<Item> ListItems
    {
        get
        {
            return _listItems;
        }
    
        set
        {
            if (_listItems == value)
            {
                return;
            }
    
            _listItems = value;
            OnPropertyChanged();
        }
    }
    
  • and you are good to go

    <local:GridViewEx SelectionMode="Multiple"  ItemsSource="{Binding ListItems}" >
           <local:GridViewEx.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </local:GridViewEx.ItemTemplate>
    </local:GridViewEx>
    
Community
  • 1
  • 1
SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
  • 1
    We have to do all these due to the lack of ancestor binding. I created a API request here (https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/9333168-ancestor-binding-missing). Please upvote if interested. – Justin XL Aug 15 '15 at 05:25
  • 1
    There is a flaw in this that if you scroll the list, the selection disappears. Not sure why/what is happening yet. – earthling Aug 18 '15 at 17:09
  • @earthling It happens due to UI virtualization. Virtualized UI probably reuses the containers and breaks the binding. – akshay2000 Jul 04 '16 at 15:36