<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>XAML</title><link>http://www.ralbu.com:80/tags/xaml</link><description>XAML</description><item><title>Building WinRT / Windows 8 Grouped items using MVVMLight</title><link>http://www.ralbu.com:80/post/2012/11/18/Building-WinRT-Grouped-items-using-MVVMLight</link><description>&lt;p&gt;One of the reasons to use MVVM pattern is the fact that you can design before having the application fully implemented. Tests are also an important part but this is not the topic of this article. When creating WinRT applications Blend is the tool you definitely should use.&amp;nbsp; When working with Grouped Items in GridView you don't have out of the box support for design using MVVM. In the following steps I will start with a new application using MvvmLight and add all the necessary bits for binding and displaying the data in UI.&lt;/p&gt;
&lt;p&gt;In order to work with MvvmLight you need to install it. Get a copy from here &lt;a href="http://mvvmlight.codeplex.com/"&gt;MvvmLight&lt;/a&gt; After installation it will add an MvvmLight project to your list of projects and some MvvmLight templates to the list of templates which you can add on an already existing project.&lt;/p&gt;
&lt;p&gt;You can download the source code of this article from GitHub &lt;a href="https://github.com/ralbu/GroupedItemsWithMvvmLight"&gt;Grouped Items with MvvmLight&lt;/a&gt;. I removed some of the code created by MvvmLight template to keep it simpler to understand. If you want to check what MvvmLight creates just a create a new MvvmLight project and check the code. &lt;br /&gt;Fire Visual Studio and create a new project. This will be an MvvmLight project.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;a href="http://www.ralbu.com/image.axd?picture=clip_image002.jpg"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-width: 0px;" title="clip_image002" alt="clip_image002" src="http://www.ralbu.com/image.axd?picture=clip_image002_thumb.jpg" border="0" height="367" width="600" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;When you chose the MvvmLight project it creates a few classes. You can use other type but you'll need to add these classes manually. &lt;br /&gt;First of all, the App.xaml file was updated with the following&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"  /&amp;gt;&lt;/pre&gt;
&lt;p&gt;It references the ViewModelLocator class added in the ViewModel folder. This is a locator class and uses SimpleIoc as a dependency injector.&lt;/p&gt;
&lt;p&gt;MainViewModel class was created in the same ViewModel folder and has WelcomeTitle property binded &amp;ndash; this was created by MvvmLight and I didn&amp;rsquo;t remove it.&lt;/p&gt;
&lt;p&gt;The MainPage.xaml updated with the DataContext reference&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;DataContext="{Binding Main, Source={StaticResource Locator}}&lt;/pre&gt;
&lt;p&gt;Here Main is the 'Main' property of type MainViewModel defined in the ViewModelLocator.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;We will create an example of products so for this purpose a new set of classes were added to the Model folder. The first one - ProductItem has Title, Price and images properties. The second one - ProductGroup has Title which is the title of the group and Items which is a collection of ProductItems for the same group.&lt;/p&gt;
&lt;p&gt;The MainViewModel class had a constructor with IDataService parameter which was created by MvvmLight. That constructor is not needed any more and we need to create a constructor with no parameters. I will explain later why we need it. In the constructor I will set the WelcomeTitle and I will populate the Products collection with data.&lt;/p&gt;
&lt;p&gt;Let's add a grid view to the MainPage&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"&amp;gt;
        &amp;lt;Grid.RowDefinitions&amp;gt;
            &amp;lt;RowDefinition Height="13*"/&amp;gt;
            &amp;lt;RowDefinition Height="51*"/&amp;gt;
        &amp;lt;/Grid.RowDefinitions&amp;gt;
        &amp;lt;TextBlock FontSize="56"
                   Text="{Binding WelcomeTitle}"
                   VerticalAlignment="Top"
                   HorizontalAlignment="Left"
                   FontFamily="Segoe UI Light" 
                   Margin="120,49,0,0" /&amp;gt;

        &amp;lt;Grid Style="{StaticResource LayoutRootStyle}" Grid.Row="1"&amp;gt;

            &amp;lt;GridView
            x:Name="itemGridView"
            AutomationProperties.AutomationId="ItemGridView"
            AutomationProperties.Name="Grouped Items"
            Grid.RowSpan="2"
            Padding="116,1,40,46"
            ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
            ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
            SelectionMode="None"
            IsSwipeEnabled="false"&amp;gt;

                &amp;lt;GridView.ItemsPanel&amp;gt;
                    &amp;lt;ItemsPanelTemplate&amp;gt;
                        &amp;lt;VirtualizingStackPanel Orientation="Horizontal"/&amp;gt;
                    &amp;lt;/ItemsPanelTemplate&amp;gt;
                &amp;lt;/GridView.ItemsPanel&amp;gt;
                &amp;lt;GridView.GroupStyle&amp;gt;
                    &amp;lt;GroupStyle&amp;gt;
                        &amp;lt;GroupStyle.HeaderTemplate&amp;gt;
                            &amp;lt;DataTemplate&amp;gt;
                                &amp;lt;Grid Margin="1,0,0,6"&amp;gt;
                                    &amp;lt;Button
                                    AutomationProperties.Name="Group Title"
                                    Style="{StaticResource TextPrimaryButtonStyle}"&amp;gt;
                                        &amp;lt;StackPanel Orientation="Horizontal"&amp;gt;
                                            &amp;lt;TextBlock Text="{Binding Title}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" /&amp;gt;
                                            &amp;lt;TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/&amp;gt;
                                        &amp;lt;/StackPanel&amp;gt;
                                    &amp;lt;/Button&amp;gt;
                                &amp;lt;/Grid&amp;gt;
                            &amp;lt;/DataTemplate&amp;gt;
                        &amp;lt;/GroupStyle.HeaderTemplate&amp;gt;
                        &amp;lt;GroupStyle.Panel&amp;gt;
                            &amp;lt;ItemsPanelTemplate&amp;gt;
                                &amp;lt;VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/&amp;gt;
                            &amp;lt;/ItemsPanelTemplate&amp;gt;
                        &amp;lt;/GroupStyle.Panel&amp;gt;
                    &amp;lt;/GroupStyle&amp;gt;
                &amp;lt;/GridView.GroupStyle&amp;gt;
            &amp;lt;/GridView&amp;gt;
        &amp;lt;/Grid&amp;gt;
&amp;lt;/Grid&amp;gt;&lt;/pre&gt;
&lt;p&gt;Grid View ItemsSource is defined using groupedItemsViewSource&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"&lt;/pre&gt;
&lt;p&gt;This is the most important part. groupedItemsViewSource is a CollectionVewSource &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;CollectionViewSource
    x:Name="groupedItemsViewSource"
    Source="{Binding Products}"
    IsSourceGrouped="true"
    ItemsPath="Items"
    d:Source="{Binding Products, Source={d:DesignInstance Type=data:MainViewModel, IsDesignTimeCreatable=True}}"/&amp;gt;&lt;/pre&gt;
&lt;p&gt;Pay attention at the &lt;span style="font-family: Courier New;" face="Courier New"&gt;&lt;span style="font-size: small;" size="2"&gt;d:Source&lt;/span&gt;.&lt;/span&gt; This is the code which binds the design view with the ViewModel. The 'd' namespace is ignored by the run time. It is used only for design time. &lt;br /&gt;The source is bindend to the Products collection of the MainViewModel. I said before that MainViewModel needs a parameter less constructor. This is the place where the constructor is used. If you don't define it then the source will not bind for design time.&lt;/p&gt;
&lt;p&gt;Now if you open the MainPage in Blend it will nicely display the design with all the images in place.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://www.ralbu.com/image.axd?picture=blend-mvvm.png"&gt;&lt;img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="blend-mvvm" alt="blend-mvvm" src="http://www.ralbu.com/image.axd?picture=blend-mvvm_thumb.png" border="0" height="322" width="600" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Source code is available on GitHub &lt;a href="https://github.com/ralbu/GroupedItemsWithMvvmLight"&gt;Grouped Items with MvvmLight&lt;/a&gt;.&lt;/p&gt;</description><pubDate>Sun, 18 Nov 2012 15:35:00 GMT</pubDate><guid isPermaLink="true">http://www.ralbu.com:80/post/2012/11/18/Building-WinRT-Grouped-items-using-MVVMLight</guid></item><item><title>ListBoxItem colors in Windows 8</title><link>http://www.ralbu.com:80/post/2012/09/01/ListBoxItem-colors-in-Windows-8</link><description>&lt;p&gt;In WPF you can override some resource in order change the colors of the ListBoxItem. For instance, overriding some standard brushes:&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;Style TargetType="ListBoxItem"&amp;gt;
    &amp;lt;Style.Resources&amp;gt;
        &amp;lt;SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/&amp;gt;
    &amp;lt;/Style.Resources&amp;gt;
&amp;lt;/Style&amp;gt;&lt;/pre&gt;
&lt;p&gt;This doesn't work with Windows 8. You'll need to update the style for each state.&lt;/p&gt;
&lt;p&gt;Blend can be used to generate the layout using &lt;em&gt;ItemContainerStyle&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;The following is the default generated &lt;em&gt;ItemContainerStyle&lt;/em&gt;:&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&amp;gt;
	&amp;lt;Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"&amp;gt;
		&amp;lt;Setter Property="Background" Value="Transparent"/&amp;gt;
		&amp;lt;Setter Property="Padding" Value="8,10"/&amp;gt;
		&amp;lt;Setter Property="HorizontalContentAlignment" Value="Left"/&amp;gt;
		&amp;lt;Setter Property="Template"&amp;gt;
			&amp;lt;Setter.Value&amp;gt;
				&amp;lt;ControlTemplate TargetType="ListBoxItem"&amp;gt;
					&amp;lt;Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"&amp;gt;
						&amp;lt;VisualStateManager.VisualStateGroups&amp;gt;
							&amp;lt;VisualStateGroup x:Name="CommonStates"&amp;gt;
								&amp;lt;VisualState x:Name="Normal"/&amp;gt;
								&amp;lt;VisualState x:Name="PointerOver"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemPointerOverBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemPointerOverForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Disabled"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemDisabledForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Pressed"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PressedBackground"/&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemPressedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
							&amp;lt;/VisualStateGroup&amp;gt;
							&amp;lt;VisualStateGroup x:Name="SelectionStates"&amp;gt;
								&amp;lt;VisualState x:Name="Unselected"/&amp;gt;
								&amp;lt;VisualState x:Name="Selected"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedUnfocused"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedDisabled"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedDisabledBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedDisabledForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedPointerOver"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedPointerOverBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedPressed"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
							&amp;lt;/VisualStateGroup&amp;gt;
							&amp;lt;VisualStateGroup x:Name="FocusStates"&amp;gt;
								&amp;lt;VisualState x:Name="Focused"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/&amp;gt;
										&amp;lt;DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Unfocused"/&amp;gt;
								&amp;lt;VisualState x:Name="PointerFocused"/&amp;gt;
							&amp;lt;/VisualStateGroup&amp;gt;
						&amp;lt;/VisualStateManager.VisualStateGroups&amp;gt;
						&amp;lt;Grid x:Name="InnerGrid" Background="Transparent"&amp;gt;
							&amp;lt;Rectangle x:Name="PressedBackground" Fill="{StaticResource ListBoxItemPressedBackgroundThemeBrush}" Opacity="0"/&amp;gt;
							&amp;lt;ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&amp;gt;
							&amp;lt;Rectangle x:Name="FocusVisualWhite" Opacity="0" StrokeDashOffset=".5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/&amp;gt;
							&amp;lt;Rectangle x:Name="FocusVisualBlack" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/&amp;gt;
						&amp;lt;/Grid&amp;gt;
					&amp;lt;/Border&amp;gt;
				&amp;lt;/ControlTemplate&amp;gt;
			&amp;lt;/Setter.Value&amp;gt;
		&amp;lt;/Setter&amp;gt;
	&amp;lt;/Style&amp;gt;
 &amp;lt;/ResourceDictionary&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Visual states should be updated in order to override the colors for the different state.&lt;/p&gt;
&lt;p&gt;There are three states group with the following visual states:&lt;/p&gt;
&lt;h3&gt;Common States&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Normal
&lt;ul&gt;
&lt;li&gt;Pressed&lt;/li&gt;
&lt;li&gt;Disabled&lt;/li&gt;
&lt;li&gt;PointerOver&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Focus States
&lt;ul&gt;
&lt;li&gt;Unfocused&lt;/li&gt;
&lt;li&gt;Focused&lt;/li&gt;
&lt;li&gt;PointerFocused&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Selection States
&lt;ul&gt;
&lt;li&gt;Unselected&lt;/li&gt;
&lt;li&gt;Selected&lt;/li&gt;
&lt;li&gt;SelectedUnfocused&lt;/li&gt;
&lt;li&gt;SelectedDisabled&lt;/li&gt;
&lt;li&gt;SelectedPointerOver&lt;/li&gt;
&lt;li&gt;SelectedPressed&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The states will update the parts list box item is composed of.&lt;/p&gt;
&lt;p&gt;The following XAML composes a list box item:&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;Grid x:Name="InnerGrid" Background="Transparent"&amp;gt;
	&amp;lt;Rectangle x:Name="PressedBackground" Fill="LightGray" Opacity="0"/&amp;gt;
	&amp;lt;ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&amp;gt;
	&amp;lt;Rectangle x:Name="FocusVisualWhite" Opacity="0" StrokeDashOffset=".5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/&amp;gt;
	&amp;lt;Rectangle x:Name="FocusVisualBlack" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/&amp;gt;
&amp;lt;/Grid&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In most cases the &lt;b&gt;InnerGrid &lt;/b&gt;background color is updated but there are occasions when PressedBackground rectangle should change its color. I will specify this explicitly.&lt;/p&gt;
&lt;p&gt;The generated template by Blend uses the key frame animation. You see that storyboard element contains elements ending in keyframes. A key frame animation is an animation that is made up by different segments of animation which transition from one value to another.&lt;/p&gt;
&lt;p&gt;There are linear key frames and discrete key frames. The linear key frames transitions smoothly from one value to another. In discrete key frames, when the time frame is reached the value is changed abruptly, therefore there is no smooth transition. In our case the discrete option works ok as we don&amp;rsquo;t have any smooth transition (unless you want to implement one).&lt;/p&gt;
&lt;p&gt;If you edit the style in Blend then when you change the background color it will add the following before the end of the storyboard element&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;ColorAnimation Duration="0" To="COLOR" Storyboard.TargetProperty="(TARGET PROPERTY).(SolidColorBrush.Color)" Storyboard.TargetName="TARGET NAME" d:IsOptimized="True"/&amp;gt;&lt;/pre&gt;
&lt;p&gt;This is a an example of linear key frames. But we will update the DiscreteObjectKeyFrame element in our implementation.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s review the states where we need to update the visual state.&lt;/p&gt;
&lt;h3&gt;Normal state&lt;/h3&gt;
&lt;p&gt;When treating the normal state we should take into account the previous state and focus state.&lt;/p&gt;
&lt;p&gt;The first time the application is opened list box item will have the color set in FocusState - Unfocused. Also this state happens if you select one item and next select another one. The previous one will take the color of the FocusState - Unfocused.&lt;/p&gt;
&lt;p&gt;Move the cursor over and the item will take the color of the Commonstate - Normal.&lt;/p&gt;
&lt;p&gt;The SelectionStates - Unselected is a trickier one and needs some explanation. Let's say we have a ListView with three items. We select one item and move the focus to (select) another component. Next, we move the focus back to our list of items but we select another item, not the one which had the selection. In this case the one which had the selection before will change its color to SelectionState - Unselected&lt;/p&gt;
&lt;h3&gt;Pressed&lt;/h3&gt;
&lt;p&gt;The pressed state happens when the user clicks on the item. In order to change the color for the CommonState &amp;ndash; Pressed the color of the PressedBackground should be changed which is the rectangle and not the LayoutRoot grid as in other situations.&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"&amp;gt;
	&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgPressed}"/&amp;gt;
&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;For the SelectionStates &amp;ndash; SelectedPressed the animation is commented out.&lt;/p&gt;
&lt;p&gt;Here are some color brushes which you need to change depending on the colors you need. I've selected basic colors (RBG) to make it easier to understand what needs to be changed.&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;SolidColorBrush x:Key="bgNormal" Color="Red" /&amp;gt;
 &amp;lt;SolidColorBrush x:Key="bgPointerOver" Color="DarkGreen" /&amp;gt;
&amp;lt;SolidColorBrush x:Key="bgSelectedPointerOver" Color="Green" /&amp;gt;
&amp;lt;SolidColorBrush x:Key="bgSelected" Color="DeepPink" /&amp;gt;
&amp;lt;SolidColorBrush x:Key="bgPressed" Color="Orange" /&amp;gt;&lt;/pre&gt;
&lt;p&gt;And here is the full style&lt;/p&gt;
&lt;pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"&gt;&amp;lt;Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"&amp;gt;
		&amp;lt;Setter Property="Background" Value="Transparent"/&amp;gt;
		&amp;lt;Setter Property="TabNavigation" Value="Local"/&amp;gt;
		&amp;lt;Setter Property="Padding" Value="8,10"/&amp;gt;
		&amp;lt;Setter Property="HorizontalContentAlignment" Value="Left"/&amp;gt;
		&amp;lt;Setter Property="Template"&amp;gt;
			&amp;lt;Setter.Value&amp;gt;
				&amp;lt;ControlTemplate TargetType="ListBoxItem"&amp;gt;
					&amp;lt;Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"&amp;gt;
						&amp;lt;VisualStateManager.VisualStateGroups&amp;gt;
							&amp;lt;VisualStateGroup x:Name="CommonStates"&amp;gt;
								&amp;lt;VisualState x:Name="Normal"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
									&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"&amp;gt;
										&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgNormal}"/&amp;gt;
									&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="PointerOver"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgPointerOver}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemPointerOverForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Disabled"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemDisabledForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Pressed"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
                                        &amp;lt;DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="PressedBackground"/&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemPressedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
                                        &amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="PressedBackground"&amp;gt;
                                            &amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgPressed}"/&amp;gt;
                                        &amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
							&amp;lt;/VisualStateGroup&amp;gt;
							&amp;lt;VisualStateGroup x:Name="SelectionStates"&amp;gt;
								&amp;lt;VisualState x:Name="Unselected"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
									&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot"&amp;gt;
										&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgNormal}"/&amp;gt;
									&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Selected"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;!--ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames--&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedUnfocused"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgSelected}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedDisabled"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedDisabledBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedDisabledForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedPointerOver"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource bgSelectedPointerOver}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="SelectedPressed"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;!--ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="InnerGrid"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedBackgroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames--&amp;gt;
										&amp;lt;ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter"&amp;gt;
											&amp;lt;DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxItemSelectedForegroundThemeBrush}"/&amp;gt;
										&amp;lt;/ObjectAnimationUsingKeyFrames&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
							&amp;lt;/VisualStateGroup&amp;gt;
							&amp;lt;VisualStateGroup x:Name="FocusStates"&amp;gt;
								&amp;lt;VisualState x:Name="Focused"&amp;gt;
									&amp;lt;Storyboard&amp;gt;
										&amp;lt;DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/&amp;gt;
										&amp;lt;DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/&amp;gt;
									&amp;lt;/Storyboard&amp;gt;
								&amp;lt;/VisualState&amp;gt;
								&amp;lt;VisualState x:Name="Unfocused"/&amp;gt;
								&amp;lt;VisualState x:Name="PointerFocused" /&amp;gt;
							&amp;lt;/VisualStateGroup&amp;gt;
						&amp;lt;/VisualStateManager.VisualStateGroups&amp;gt;
						&amp;lt;Grid x:Name="InnerGrid" Background="Transparent"&amp;gt;
							&amp;lt;Rectangle x:Name="PressedBackground" Fill="{StaticResource ListBoxItemPressedBackgroundThemeBrush}" Opacity="0"/&amp;gt;
							&amp;lt;ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&amp;gt;
							&amp;lt;Rectangle x:Name="FocusVisualWhite" Opacity="0" StrokeDashOffset=".5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1"/&amp;gt;
							&amp;lt;Rectangle x:Name="FocusVisualBlack" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1"/&amp;gt;
						&amp;lt;/Grid&amp;gt;
					&amp;lt;/Border&amp;gt;
				&amp;lt;/ControlTemplate&amp;gt;
			&amp;lt;/Setter.Value&amp;gt;
		&amp;lt;/Setter&amp;gt;
&amp;lt;/Style&amp;gt;&lt;/pre&gt;
&lt;p&gt;Copy the style in your resource file and update the brushes as you need.&lt;/p&gt;</description><pubDate>Sat, 01 Sep 2012 14:35:00 GMT</pubDate><guid isPermaLink="true">http://www.ralbu.com:80/post/2012/09/01/ListBoxItem-colors-in-Windows-8</guid></item></channel></rss>