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>