Created
June 27, 2017 18:37
-
-
Save arthurrump/fc7b8c48f758127563c8645816684e22 to your computer and use it in GitHub Desktop.
AdaptiveGridView.ItemContainerStyleSelector doesn't work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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