Skip to content

Instantly share code, notes, and snippets.

@kimsk
Created June 7, 2013 03:34
Show Gist options
  • Save kimsk/5726910 to your computer and use it in GitHub Desktop.
Save kimsk/5726910 to your computer and use it in GitHub Desktop.
ผมสร้างคลาสใหม่ที่ inherit มาจาก GridView แล้วก็ใน PrepareContainerForItemOverride ก็ใส่โค้ดเช็คว่าเป็นตัวที่เท่าไหร่ ถ้าเป็นเลขคู่ก็เปลี่ยนสีแบ็กกราวนด์เป็นสีนึง ข้อเสียคือต้องบังคับให้ GridView มีจำนวนแถวเป็นเลขคู่ มีตัวอย่าง XAML ในอีกไฟล์ครับ
public class AlternateColorGridView : GridView
{
protected override void PrepareContainerForItemOverride(Windows.UI.Xaml.DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
int index = ItemContainerGenerator.IndexFromContainer(element);
var gridViewItem = element as GridViewItem;
if (index % 2 == 0)
{
gridViewItem.Background = new SolidColorBrush(Colors.Cyan);
}
else
{
gridViewItem.Background = new SolidColorBrush(Colors.Magenta);
}
}
}
<Page
x:Class="Win8ThaiDevAlternateGridView.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Win8ThaiDevAlternateGridView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Key="itemDataTemplate">
<Border Background="Blue" BorderBrush="Yellow"
Margin="10" Padding="10">
<TextBlock Foreground="Yellow" Text="{Binding}"
FontSize="32"/>
</Border>
</DataTemplate>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<local:AlternateColorGridView x:Name="alternateGridView" ItemTemplate="{StaticResource itemDataTemplate}"
Height="500"
Width="500">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid ItemHeight="120" ItemWidth="120" MaximumRowsOrColumns="4" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</local:AlternateColorGridView>
</Grid>
</Page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment