Animate Grid Width and Height when a TextBox is Clicked - xaml

The following code successfully changes the width and height of a Grid when myTextBox is clicked. What I would like to be able to do is animate animate the transition when the TextBox is clicked.
How can I animate the following transition when myTextBox is clicked/InFocused?
<Grid HorizontalAlignment="Center" Margin="0,-180,0,0">
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Background" Value="White"/>
<Setter Property="Width" Value="350"/>
<Setter Property="Height" Value="150"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myTextBox, Path=IsFocused}" Value="True">
<Setter Property="Background" Value="#FFF7F7F7" />
<Setter Property="Width" Value="320" />
<Setter Property="Height" Value="120" />
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>

You can put a Storyboards under TextBox.Triggers, that should run when it GotFocus/LostFocus:
<!-- Your grid, width and height of which should be animated -->
<Grid x:Name="myGrid" HorizontalAlignment="Center" >
<Grid.Style>
<Style TargetType="{x:Type Grid}">
<Setter Property="Background" Value="White"/>
<Setter Property="Width" Value="350"/>
<Setter Property="Height" Value="150"/>
</Style>
</Grid.Style>
<!-- Your textbox (inside grid), that should trigger animation when Got/Lost focus -->
<TextBox x:Name="myTextBox">
<TextBox.Triggers>
<!-- When TextBox got focus -->
<EventTrigger RoutedEvent="TextBox.GotFocus">
<BeginStoryboard>
<Storyboard>
<!-- Animating width -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Width)"
From="350" To="320" Duration="0:0:0.1">
</DoubleAnimation>
<!-- Animating height -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Height)"
From="150" To="120" Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<!-- When TextBox lost focus -->
<EventTrigger RoutedEvent="TextBox.LostFocus">
<BeginStoryboard>
<Storyboard>
<!-- Animating width -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Width)"
From="320" To="350" Duration="0:0:0.1">
</DoubleAnimation>
<!-- Animating height -->
<DoubleAnimation Storyboard.TargetName="myGrid"
Storyboard.TargetProperty="(Grid.Height)"
From="120" To="150" Duration="0:0:0.1">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
</Grid>
TextBox may not be inside Grid, that you animate, I put inside just for example.
Example tested and works fine on me.

Related

Fire click on templated button

I want to create a custom button reveal template. I won't use Style={ThemeResource ButtonRevealStyle} for some reason. So, I've added ListViewItem inside button control template.
<Style TargetType="Button" x:Key="ButtonListView">
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Control.IsTemplateFocusTarget="True">
<StackPanel>
<ListViewItem>
<ContentPresenter></ContentPresenter>
</ListViewItem>
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Style="{ThemeResource ButtonListView}" Click="list_refresh">
<TextBlock Text="Button Content"/>
</Button>
Illustration:
But click event doesn't fire when I clicked on it. I think it fires on ListViewItem, not the button. And the most confusing thing is, it could fires using keyboard.
Fire click on templated button
As #Vincent said ListViewItem is the first one to capture your click event, for your scenario, we suggest you custom button reveal style and use ListViewItem reveal theme resource. Or use Button Tapped event to replace.
For exampl:
<Style x:Key="CudtomButtonRevealStyle" TargetType="Button">
<Setter Property="Background" Value="{ThemeResource ListViewItemRevealBackground}" />
<Setter Property="Foreground" Value="{ThemeResource ButtonForeground}" />
<Setter Property="BorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrush}" />
<Setter Property="BorderThickness" Value="{ThemeResource ButtonRevealBorderThemeThickness}" />
<Setter Property="Padding" Value="{ThemeResource ButtonPadding}" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontWeight" Value="Normal" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="UseSystemFocusVisuals" Value="{StaticResource UseSystemFocusVisuals}" />
<Setter Property="FocusVisualMargin" Value="-3" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid
x:Name="RootGrid"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
<ContentPresenter
x:Name="ContentPresenter"
Padding="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
AutomationProperties.AccessibilityView="Raw"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}"
CornerRadius="{TemplateBinding CornerRadius}" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<VisualState.Setters>
<Setter Target="RootGrid.(RevealBrush.State)" Value="PointerOver" />
<Setter Target="RootGrid.Background" Value="{ThemeResource ListViewItemRevealBackgroundPointerOver}" />
<Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPointerOver}" />
<Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundPointerOver}" />
</VisualState.Setters>
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<VisualState.Setters>
<Setter Target="RootGrid.(RevealBrush.State)" Value="Pressed" />
<Setter Target="RootGrid.Background" Value="{ThemeResource ListViewItemRevealBackgroundPressed}" />
<Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ListViewItemRevealBorderBrushPressed}" />
<Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundPressed}" />
</VisualState.Setters>
<Storyboard>
<PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
</Storyboard>
</VisualState>
<VisualState x:Name="Disabled">
<VisualState.Setters>
<Setter Target="RootGrid.Background" Value="{ThemeResource ButtonRevealBackgroundDisabled}" />
<Setter Target="ContentPresenter.BorderBrush" Value="{ThemeResource ButtonRevealBorderBrushDisabled}" />
<Setter Target="ContentPresenter.Foreground" Value="{ThemeResource ButtonForegroundDisabled}" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Usage
<Button
VerticalAlignment="Bottom"
Click="Button_Click"
Content="Connect"
Style="{ThemeResource CudtomButtonRevealStyle}" />
This is the focus problem. When you use a keyboard, you could use Tab to move focus to Button, so it fires the click event.
When you use a pointer, and press on the Button, ListViewItem is the first one to capture your click event, you can add a Tapped event to ListViewItem to prove it.
So If you insist want the Button to respond to your pointer, I think you can remove the ListViewItem.
<ControlTemplate TargetType="Button">
<Grid Control.IsTemplateFocusTarget="True">
Add button content here
</Grid>
</ControlTemplate>

Styling depending on screen orientation in Xamarin forms

I am styling on phone and tablet but how can I add the option also for orientation? this is all for portrait but how can I add option for horizontal orientation?
<Style TargetType="Grid" x:Key="DocType">
<Setter Property="HeightRequest">
<Setter.Value>
<OnIdiom Phone="50" Tablet="80" />
</Setter.Value>
</Setter>
<Setter Property="BackgroundColor" Value="#F4F5F7"/>
</Style>
You can use OrientationStateTrigger with VisualStateManager here is an example:
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState
x:Name="Landscape">
<VisualState.StateTriggers>
<OrientationStateTrigger Orientation="Landscape" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Blue" />
</VisualState.Setters>
</VisualState>
<VisualState
x:Name="Portrait">
<VisualState.StateTriggers>
<OrientationStateTrigger Orientation="Portrait" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
Source
EDIT
VisualStateManager inside a style, combined with OnIdiom
<Style TargetType="Grid" x:Key="DocType">
<Setter Property="HeightRequest">
<Setter.Value>
<OnIdiom Phone="50" Tablet="80" />
</Setter.Value>
</Setter>
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup>
<VisualState x:Name="Landscape">
<VisualState.StateTriggers>
<OrientationStateTrigger Orientation="Landscape" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="BackgroundColor">
<Setter.Value>
<OnIdiom Phone="Blue" Tablet="Green" />
</Setter.Value>
</Setter>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Portrait">
<VisualState.StateTriggers>
<OrientationStateTrigger Orientation="Portrait" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Property="BackgroundColor">
<Setter.Value>
<OnIdiom Phone="Yellow" Tablet="Purple" />
</Setter.Value>
</Setter>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
in this example:
Mode
Tablet
Phone
Landscape
Green
Blue
Portrait
Purple
Yellow

Hide Default checkbox inside MultiSelect ListView (UWP)

I have a ListView like this
<ListView ItemsSource="{x:Bind DDLItemsSource, Mode=OneWay}" Background="Transparent" VerticalAlignment="Stretch"
SelectionChanged="StudentsList_SelectionChanged" x:Name="StudentsList" SelectionMode="Multiple" HorizontalAlignment="Stretch" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="BorderThickness" Value="0 0 0 1"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
When this Listview Renders on-page, it comes with Default Checkbox inside each item. I want to hide those Checkboxes and show only data. how can I achieve this?
This is how the ListView looks now
Hide Default checkbox inside MultiSelect ListView (UWP)
That could be approached easily by edit the <Style TargetType="ListViewItem" x:Key="ListViewItemExpanded">,(in generic.xaml file) you could find the checkbox was made with MultiSelectSquare Border, we just add the edit Normal VisualState like the following, and the checkbox will be hidden when Multiple SelectionMode.
<VisualState x:Name="Normal">
<Storyboard>
<PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
Usage
<ListView
Name="TestList"
IsItemClickEnabled="True"
ItemClick="ListView_ItemClick"
ItemContainerStyle="{StaticResource ListViewItemExpanded}"
SelectionMode="Multiple"
Visibility="Visible"
>
You can fix it by changing ListView to ListBox:
<ListBox
ItemsSource="{x:Bind DDLItemsSource, Mode=OneWay}"
Background="Transparent"
VerticalAlignment="Stretch"
SelectionChanged="StudentsList_SelectionChanged"
x:Name="StudentsList"
SelectionMode="Multiple"
HorizontalAlignment="Stretch" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="BorderThickness" Value="0 0 0 1"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>

UWP ListView - DragOver not called anymore

I'm facing an issue that I can't solve. I have a ListView and I need to customize item style when the user drag an object over the list view item. Due to the complexity of my states, I don't want to keep the ListViewItemPresenter of the extended ListViewItem style. So when i remove that presenter and define my own style (basically a classic content presenter + overlay + underlay + VisualStateGroups to define animations), I don't recieve the DragOver and Drop event anymore on my list item objects. I don't understand why ? If i keep the default item container style it works fine... What did i miss?
Let me go a bit further with some code. Here is my List view that define ItemTemplate and ItemContainerStyle :
<ListView x:Name="notebookListView"
ItemsSource="{x:Bind NoteBookItems}"
ItemTemplate="{StaticResource NotebookItemListViewTemplate}"
ItemContainerStyle="{StaticResource notebookItemContainerStyle}"
IsItemClickEnabled="True"
SelectionMode="Single"
AllowDrop="True" CanReorderItems="False" CanDragItems="True"
DragItemsStarting="NotebookListView_DragItemsStarting"
DragItemsCompleted="NotebookListView_DragItemsCompleted"
ItemClick="NotebookItem_Click" />
Here is the item template that define the DragOver and Drop method:
<DataTemplate x:Name="NotebookItemListViewTemplate" x:DataType="model:NotebookItem">
<Grid x:Name="notebook" HorizontalAlignment="Left" Height="48" Width="320"
AllowDrop="True"
DragOver="DragOverNotebookItem"
Drop="DropInNotebookItem">
[...]
</Grid>
</DataTemplate>
And finaly my extended item container style:
<Style x:Key="notebookItemContainerStyle" TargetType="ListViewItem">
<!-- all default setter properties ... -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<!-- Here is my customization with VisualState to catch PointerOver and other events above my list items -->
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="overlay" Duration="0" To="{StaticResource ColorUIBlackAlpha04}" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" />
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="overlay" Duration="0" To="{StaticResource ColorUIBlackAlpha12}" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" />
</Storyboard>
</VisualState>
<VisualState x:Name="PressedSelected">
<Storyboard>
<ColorAnimation Storyboard.TargetName="overlay" Duration="0" To="{StaticResource ColorUIBlackAlpha12}" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" />
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<ColorAnimation Storyboard.TargetName="overlay" Duration="0" To="Transparent" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<!-- and a Grid that contains the definition of the item content -->
<Grid>
<Rectangle x:Name="underlay" Stroke="Transparent" Fill="Transparent" Margin="0" />
<ContentPresenter Margin="{TemplateBinding Padding}"
Background="Transparent"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
<Rectangle x:Name="overlay" Stroke="Transparent" Fill="Transparent" Margin="0" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
With that code, the DragOver event of my item is never triggered. But if i use the default item container style with the default ListViewItemPresenter it works fine. I don't understand why... Could anyone help me to understand what happens ?
Here is the default list view item container style, with the ListViewItemPresenter instead of my custo:
<Style x:Key="notebookItemContainerStyle" TargetType="ListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
<Setter Property="Background" Value="{ThemeResource ListViewItemBackground}"/>
<Setter Property="Foreground" Value="{ThemeResource ListViewItemForeground}"/>
<Setter Property="TabNavigation" Value="Local"/>
<Setter Property="IsHoldingEnabled" Value="True"/>
<Setter Property="Padding" Value="12,0,12,0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/>
<Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/>
<Setter Property="AllowDrop" Value="True"/>
<Setter Property="UseSystemFocusVisuals" Value="True"/>
<Setter Property="FocusVisualMargin" Value="0"/>
<Setter Property="FocusVisualPrimaryBrush" Value="{ThemeResource ListViewItemFocusVisualPrimaryBrush}"/>
<Setter Property="FocusVisualPrimaryThickness" Value="2"/>
<Setter Property="FocusVisualSecondaryBrush" Value="{ThemeResource ListViewItemFocusVisualSecondaryBrush}"/>
<Setter Property="FocusVisualSecondaryThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<ListViewItemPresenter CheckBrush="{ThemeResource ListViewItemCheckBrush}"
ContentMargin="{TemplateBinding Padding}"
CheckMode="{ThemeResource ListViewItemCheckMode}"
ContentTransitions="{TemplateBinding ContentTransitions}"
CheckBoxBrush="{ThemeResource ListViewItemCheckBoxBrush}"
DragForeground="{ThemeResource ListViewItemDragForeground}"
DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}"
DragBackground="{ThemeResource ListViewItemDragBackground}"
DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}"
FocusBorderBrush="{ThemeResource ListViewItemFocusBorderBrush}"
FocusSecondaryBorderBrush="{ThemeResource ListViewItemFocusSecondaryBorderBrush}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
Control.IsTemplateFocusTarget="True"
PointerOverForeground="{ThemeResource ListViewItemForegroundPointerOver}"
PressedBackground="{StaticResource UIBlackAlpha12}"
PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackground}"
PointerOverBackground="{StaticResource UIBlackAlpha04}"
ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}"
SelectedPressedBackground="Transparent"
SelectionCheckMarkVisualEnabled="{ThemeResource ListViewItemSelectionCheckMarkVisualEnabled}"
SelectedForeground="{ThemeResource ListViewItemForegroundSelected}"
SelectedPointerOverBackground="Transparent"
SelectedBackground="Transparent"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I could reproduce your issue, but I don't know the reason that caused this issue. I will continue to study this issue. Currently, there is a workaround for you. I found you want to modify the color of underlay overlay Rectangle. You could realize this in the code behind rather than in xaml with Visualtreehelper.
private void MYListview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
StackPanel test = MyFindListViewChildByName(MYListview.ContainerFromItem(MYListview.SelectedItem), "MyTest") as StackPanel;
test.Background = new SolidColorBrush(Colors.White);
}
public static DependencyObject MyFindListViewChildByName(DependencyObject parant, string ControlName)
{
int count = VisualTreeHelper.GetChildrenCount(parant);
for (int i = 0; i < count; i++)
{
var MyChild = VisualTreeHelper.GetChild(parant, i);
if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == ControlName)
return MyChild;
var FindResult = MyFindListViewChildByName(MyChild, ControlName);
if (FindResult != null)
return FindResult;
}
return null;
}
You could use VisualTreeHelper to get underlay overlay Rectangle instance then create animation for them. You could invoke those animation manually in ListViewItem event handler that match xaml VisualStateGroup.

ListBox Background Color (XAML/WinRT/Metro)

I'm trying to change the background color on a "ListBox" on a WinRT page (XAML). When I use the "Background" property, it changes the background how I want it when the control doesn't have the focus. When it gets the focus, it changes to White and I can't figure out how to override it.
My question, how to I force the background of the ListBox to always be Gray whether it's selected/has focus or not?
XAML #1:
<ListBox x:Name="ListBoxMenu" Background="LightGray" Grid.Row="0" Grid.Column="0" Margin="0,0,0,0">
<ListBoxItem>Menu Item 1</ListBoxItem>
<ListBoxItem>Menu Item 2</ListBoxItem>
<ListBoxItem>Menu Item 3</ListBoxItem>
</ListBox>
XAML #2 (with each item also set):
<ListBox x:Name="ListBoxMenu" Background="LightGray" Grid.Row="0" Grid.Column="0" Height="124" VerticalAlignment="Top">
<ListBoxItem Background="LightGray">Menu Item 1</ListBoxItem>
<ListBoxItem Background="LightGray">Menu Item 2</ListBoxItem>
<ListBoxItem Background="LightGray">Menu Item 3</ListBoxItem>
</ListBox>
As temporary solution, I set the ListBox to only be a hard coded height, then used a border on that column to fill in the rest of the space with LightGray. I really would like to just always set the Background color on the ListBox though, is this possible?
You can just put some colour brush overrides in your XAML resource file to override the default ListBox control template colours.
<SolidColorBrush x:Key="ListBoxBackgroundThemeBrush" Color="Transparent" />
<SolidColorBrush x:Key="ListBoxFocusBackgroundThemeBrush" Color="Transparent" />
Use Visual Studio Blend 2012 and edit the ListBox ItemTemplate or it's template, which will create a hard copy in the XAML, where you can edit it's properties.
I ran into the same issue and I used the help of Visual studio Blend. Hope this helps.
Add a style to the ListBoxMenu as follows:
<ListBox x:Name="ListBoxMenu" Style="{StaticResource ListBoxStyle1} Background="LightGray" Grid.Row="0" Grid.Column="0" Height="124" VerticalAlignment="Top">
<ListBoxItem Background="LightGray">Menu Item 1</ListBoxItem>
<ListBoxItem Background="LightGray">Menu Item 2</ListBoxItem>
<ListBoxItem Background="LightGray">Menu Item 3</ListBoxItem>
</ListBox>
Then specify the styling as follows:
<Style x:Key="ListBoxStyle1" TargetType="ListBox">
<Setter Property="Foreground" Value="{StaticResource ListBoxForegroundThemeBrush}"/>
<Setter Property="Background" Value="{StaticResource ListBoxBackgroundThemeBrush}"/>
<Setter Property="BorderBrush" Value="{StaticResource ListBoxBorderThemeBrush}"/>
<Setter Property="BorderThickness" Value="{StaticResource ListBoxBorderThemeThickness}"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Disabled"/>
<Setter Property="ScrollViewer.IsHorizontalRailEnabled" Value="True"/>
<Setter Property="ScrollViewer.VerticalScrollMode" Value="Enabled"/>
<Setter Property="ScrollViewer.IsVerticalRailEnabled" Value="True"/>
<Setter Property="ScrollViewer.ZoomMode" Value="Disabled"/>
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/>
<Setter Property="ScrollViewer.BringIntoViewOnFocusChange" Value="True"/>
<Setter Property="IsTabStop" Value="False"/>
<Setter Property="TabNavigation" Value="Once"/>
<Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
<Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource AppBarBackgroundThemeBrush}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="LayoutRoot">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ListBoxDisabledForegroundThemeBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ScrollViewer">
<DiscreteObjectKeyFrame KeyTime="0" Value="Black"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ScrollViewer x:Name="ScrollViewer">
<ItemsPresenter/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The above sample would replace the background of you List box container to Black when the focus is set to the ListBox.
If you need some more help on customizing the colors of Items in a ListBox, ListView or GridView, they all work on the same principle, just be sure to update the TargetType properties, I'd recommend having a look at Vito DeMercurio's blog post Styling a GridViewItem in WinRT