Skip to content

Instantly share code, notes, and snippets.

@arthurrump
Created June 27, 2017 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurrump/fc7b8c48f758127563c8645816684e22 to your computer and use it in GitHub Desktop.
Save arthurrump/fc7b8c48f758127563c8645816684e22 to your computer and use it in GitHub Desktop.
AdaptiveGridView.ItemContainerStyleSelector doesn't work
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls"
mc:Ignorable="d">
<Page.Resources>
<local:ThingStyleSelector x:Key="ThingStyleSelector">
<local:ThingStyleSelector.EvenStyle>
<Style TargetType="GridViewItem">
<Setter Property="Background" Value="Red"/>
</Style>
</local:ThingStyleSelector.EvenStyle>
<local:ThingStyleSelector.OddStyle>
<Style TargetType="GridViewItem">
<Setter Property="Background" Value="Green"/>
</Style>
</local:ThingStyleSelector.OddStyle>
</local:ThingStyleSelector>
</Page.Resources>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<toolkit:AdaptiveGridView DesiredWidth="100" ItemContainerStyleSelector="{StaticResource ThingStyleSelector}" ItemsSource="{x:Bind Things}">
<toolkit:AdaptiveGridView.ItemTemplate>
<DataTemplate x:DataType="local:Thing">
<TextBlock Text="{x:Bind Num}" />
</DataTemplate>
</toolkit:AdaptiveGridView.ItemTemplate>
</toolkit:AdaptiveGridView>
<GridView ItemContainerStyleSelector="{StaticResource ThingStyleSelector}" ItemsSource="{x:Bind Things}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Thing">
<TextBlock Text="{x:Bind Num}" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>
</Page>
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Things = new ObservableCollection<Thing>
{
new Thing(null),
new Thing(1),
new Thing(1),
new Thing(2),
new Thing(3),
new Thing(3),
new Thing(4),
new Thing(6)
};
}
ObservableCollection<Thing> Things { get; }
}
public class Thing
{
public Thing(int? num)
{
Num = num;
}
public int? Num { get; set; }
}
public class ThingStyleSelector : StyleSelector
{
public Style EvenStyle { get; set; }
public Style OddStyle { get; set; }
protected override Style SelectStyleCore(object item, DependencyObject container)
{
if (item is Thing t)
{
if ((t?.Num ?? 0) % 2 == 0)
return EvenStyle;
else
return OddStyle;
}
return base.SelectStyleCore(item, container);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment