class
CustomStyleSelecter : StyleSelector
{
protected
override
Style SelectStyleCore(
object
item, DependencyObject container)
SolidColorBrush highlight = (item
as
ListVwItem).Highlight;
Style style =
new
Style(
typeof
(ListViewItem));
style.Setters.Add(
Setter(Control.BackgroundProperty, highlight));
return
style;
}
public
SolidColorBrush Highlight
get
if
(Index % 2 == 0)
SolidColorBrush(Colors.Brown);
else
SolidColorBrush(Colors.Wheat);
static
int
_LastItemIndex = -1;
_Index = -1;
Index
(_Index == -1)
_Index = ++_LastItemIndex;
_Index;
<
Page.Resources
>
local:CustomStyleSelecter
x:Key
=
"customStyleSelector"
/>
</
ListView
x:Name
"gridView"
ItemsSource
"{Binding Items}"
ItemContainerStyleSelector
"{StaticResource customStyleSelector}"
BorderBrush
"Cyan"
BorderThickness
"3"
ListView.ItemTemplate
DataTemplate
StackPanel
Background
"{Binding Highlight}"
TextBlock
Text
"{Binding Txt}"
HorizontalAlignment
"Stretch"
Page
x:Class
"wrt.WrtPage"
xmlns
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
"http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local
"using:wrt"
xmlns:d
"http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
"http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
"d"
Grid
"{StaticResource ApplicationPageBackgroundThemeBrush}"
Orientation
"Vertical"
<!--<Image Source="{Binding ImageUri}" Height="60" Width="60"/>-->
namespace wrt
class CustomStyleSelecter : StyleSelector
protected override Style SelectStyleCore(object item, DependencyObject container)
SolidColorBrush highlight = (item as ListVwItem).Highlight;
Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(Control.BackgroundProperty, highlight));
return style;
class ListVwItem
static int _LastItemIndex = -1;
int _Index = -1;
public int Index
if (_Index == -1)
return _Index;
public Uri ImageUri { get; set; }
public string Txt { get; set; }
public SolidColorBrush Highlight
if (Index % 2 == 0)
return new SolidColorBrush(Colors.Brown);
return new SolidColorBrush(Colors.Wheat);
class ListVwModel
public List<
ListVwItem
> Items { get; set; }
public ListVwModel()
//Dummy Data
Items = new List<
>();
Items.Add(new ListVwItem() { Txt = "Item 1" });
Items.Add(new ListVwItem() { Txt = "Item 2" });
Items.Add(new ListVwItem() { Txt = "Item 3" });
Items.Add(new ListVwItem() { Txt = "Item 4" });
Items.Add(new ListVwItem() { Txt = "Item 5" });
Items.Add(new ListVwItem() { Txt = "Item 6" });
Items.Add(new ListVwItem() { Txt = "Item 7" });
Items.Add(new ListVwItem() { Txt = "Item 8" });
Items.Add(new ListVwItem() { Txt = "Item 9" });
public sealed partial class WrtPage : Page
public WrtPage()
this.InitializeComponent();
this.DataContext = new ListVwModel();
protected override void OnNavigatedTo(NavigationEventArgs e)
Naomi N edited Revision 1. Comment: Minor edit
Sachin S edited Original. Comment: Added Pic
Congrats on winning the gold medal in the August TechNet Guru contest! blogs.technet.com/.../technet-guru-awards-august-2013.aspx
Thanks Ed!