change layout based on orientation detect in xaml Windows app - xaml

I am trying to alter my views based on landscape and portrait orientation.
I tried code from couple of places, eg:
How to detect orientation changes and change layout?
https://social.msdn.microsoft.com/Forums/en-US/a2064993-e4a9-4a58-8498-ef03ed9403f4/how-to-set-grid-row-property-in-visualstatemanager-in-windows8-app?forum=winappswithcsharp
but it does not work for me. Not sure what I am doing wrong.? I am testing it from a Simulator.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="18*" />
</Grid.RowDefinitions>
<Grid/>
<Grid Grid.Row=1/>
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<RowDefinition Height="4*" />
<RowDefinition Height="14*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" x:Name="PageTotals" />
<Grid Grid.Row="1" x:Name="PageDetails" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ApplicationViewStates">
<VisualState x:Name="Filled"/>
<VisualState x:Name="FullScreenPortrait"/>
<VisualState x:Name="Snapped"/>
<VisualState x:Name="FullScreenLandscape">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTotals"
Storyboard.TargetProperty="(Grid.Column)">
<DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTotals"
Storyboard.TargetProperty="(Grid.RowSpan)">
<DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageDetails"
Storyboard.TargetProperty="(Grid.Column)">
<DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageDetails"
Storyboard.TargetProperty="(Grid.RowSpan)">
<DiscreteObjectKeyFrame KeyTime="0" Value="2"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>

The automatic triggers for VisualStates was something introduced with the LayoutAwarePage in Windows 8. However with Windows 8.1, the notion of Snapped screen got removed and the guidance was to design your app for 320px (setting in manifest) or 500px (default) as the minimum size. The automatic triggers on orientation changes were also removed.
There are however samples of developers who tried to reproduce the Windows 8 behavior, like this post from Jeremy Likness. You can still write/use code similar to his implementation on the existing 8.1 API's to test the orientation and if your app is full screen and then go to the desired state yourself with VisualStateManager.GoToState.
private static void SetLayout(Control control)
{
var orientation = ApplicationView.GetForCurrentView().Orientation;
string newMode;
if (orientation == ApplicationViewOrientation.Landscape)
{
newMode = ApplicationView.GetForCurrentView().IsFullScreen ? "FullScreenLandscape" : "Filled";
}
else
{
newMode = Window.Current.Bounds.Width <= 500 ? "Snapped" : "FullScreenPortrait";
}
if (newMode == GetLastOrientation(control))
{
return;
}
VisualStateManager.GoToState(control, newMode, true);
SetLastOrientation(control, newMode);
}
Source: Jeremy's post.

Related

Windows 8 button disappearing when mouse over

I have a settings control as follows (I promise I didn't really call my buttons button_1 and button_2):
<UserControl
x:Class="App1.SettingsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="646">
<Border BorderBrush="#FF590151" BorderThickness="1">
<Grid Background="White" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Background="Aqua" Grid.Row="0">
<Grid Margin="40,20,17,13">
<Grid.Transitions>
<TransitionCollection>
<EntranceThemeTransition FromHorizontalOffset="50" />
</TransitionCollection>
</Grid.Transitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Click="Button_Click_1" Margin="0,3,0,0" Grid.Column="0"
HorizontalAlignment="Left" Style="{StaticResource BackButtonStyle}"/>
<TextBlock Margin="10,5,0,0" Grid.Column="1" FontFamily="Segoe UI"
FontWeight="SemiLight" FontSize="24.6667" Text="Settings" HorizontalAlignment="Left" />
<Image Source="/Assets/icon.png" HorizontalAlignment="Right" Grid.Column="2" Margin="0,0,6,0" />
</Grid>
</Grid>
<Grid Grid.Row="1" Margin="40,24,23,0" VerticalAlignment="Top">
<Grid.Transitions>
<TransitionCollection>
<EntranceThemeTransition FromHorizontalOffset="120" />
</TransitionCollection>
</Grid.Transitions>
<Button x:Name="button2" Foreground="Black" BorderThickness="1"
Click="button2_Click">Reset Difficulty</Button>
</Grid>
</Grid>
</Border>
</UserControl>
It works fine, but when I hover over button2 it disappears. Why is this?
You need to customize the PointerOver visual state of button template and apply custom style to button.
Check out button styles and templates
Default one is
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverBackgroundThemeBrush}" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverForegroundThemeBrush}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
To eliminate effect simply comment out it.
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverBackgroundThemeBrush}" />
</ObjectAnimationUsingKeyFrames>
<!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverForegroundThemeBrush}" />
</ObjectAnimationUsingKeyFrames>-->
</Storyboard>
</VisualState>
There one more way is to change ButtonPointerOverForegroundThemeBrush resource but it will be effected to whole project.
<SolidColorBrush x:Key="ButtonPointerOverForegroundThemeBrush" Color="Black" />

Metro App Change ListView Selected Item Content Foreground

I have a ListView and modified it's DataTemplate with 2 TextBlocks.
The first TextBlock contains a Heading, the second a Sub-Heading.
I style the 2 TextBlocks with different colours.
Here's an example of the ListViewItem in Normal view.
Here's an example of the ListViewItem in Selected view.
So my question is how do I change the Foreground colours of the TextBlocks in Selected views? Hoping to do this in the xaml. I've tried setting different brushes, which work for items that haven't explicitly been styled.
Not sure how to handle this scenario.
You can use visual states.
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="txtOne" Grid.Row="0" Foreground="Green"/>
<TextBlock x:Name="txtTwo" Grid.Row="1" Foreground="Gray"/>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected"/>
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtOne" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="Red"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtTwo" Storyboard.TargetProperty="Foreground">
<DiscreteObjectKeyFrame KeyTime="0" Value="Yellow"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
You don't need to play with the visual state.
In your ResourceDictionary, set a value for these brushes "ListBoxItemSelectedBackgroundThemeBrush", "ListBoxItemSelectedPointerOverBackgroundThemeBrush", "ListBoxFocusBackgroundThemeBrush". It will override the default brushes of your application.
Example:
<!-- Overrides default ListBox brushes -->
<SolidColorBrush x:Key="ListBoxItemSelectedBackgroundThemeBrush" Color="{StaticResource GreenColor}" />
<SolidColorBrush x:Key="ListBoxItemSelectedPointerOverBackgroundThemeBrush" Color="{StaticResource LightGreenColor}" />
<SolidColorBrush x:Key="ListBoxFocusBackgroundThemeBrush" Color="Transparent" />
Here is a usefull link when developping in WinRt, which references the brushes name, for the default controls of winRt.
WinRt default brushes names and values
Thanks to some researching and thinking out of the box, found a suitable solution that works:
Metro App ListView SelectedItem Selected VisualState
I can see this being handy for a couple of other scenarios as well.

Slider component in WP8 won't move

I have a Slider component in my WP7 and WP8 app. In WP7, the Slider moves according to its Scheduler, but in WP8, it simply just won't move.
The Slider is styled, and I know there is some changes in WP8. First of all, what exactly are those changes and what do I change in code? The documentation from Microsoft is poor. Anyone got an idea of what this can be, maybe other than the changes from Microsoft?
And even if I set the default value to something, the thumb will always be in its starting position. I can't move it either.
For me, this applies to a various different styles, e.g. from Mifrosofts own Slider Styles and Templates.
This one has a template:
<Slider x:Name="Slider" IsHitTestVisible="true" Value="{Binding SliderValue, Mode=TwoWay}" Maximum="100" VerticalAlignment="Center" Margin="0,24,0,0" Template="{StaticResource SliderControlTemplate1}" />
Here is the template:
<ControlTemplate x:Key="SliderControlTemplate1" TargetType="Slider">
<Grid Background="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Disabled">
<Storyboard>
<DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="HorizontalTrack"/>
<DoubleAnimation Duration="0" To="0.1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalTrack"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="HorizontalFill">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="VerticalFill">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid x:Name="HorizontalTemplate" Margin="{StaticResource PhoneHorizontalMargin}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="12"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle x:Name="HorizontalFill" Fill="{TemplateBinding Foreground}" Height="12" IsHitTestVisible="False" Margin="0"/>
<Rectangle x:Name="HorizontalTrack" Grid.Column="2" Fill="{TemplateBinding Background}" Height="12" IsHitTestVisible="False" Margin="0" Opacity="0.2"/>
<RepeatButton x:Name="HorizontalTrackLargeChangeDecreaseRepeatButton" IsTabStop="False" Template="{StaticResource PhoneSimpleRepeatButton}"/>
<RepeatButton x:Name="HorizontalTrackLargeChangeIncreaseRepeatButton" Grid.Column="2" IsTabStop="False" Template="{StaticResource PhoneSimpleRepeatButton}"/>
<Thumb x:Name="HorizontalCenterElement" Grid.Column="1" Height="12" Margin="0" Width="12">
<Thumb.Template>
<ControlTemplate>
<Grid ManipulationDelta="ProgressBarManipulationDelta" Width="46" Height="46" VerticalAlignment="Center" HorizontalAlignment="Center">
<Image Source="/slider.png"/>
</Grid>
</ControlTemplate>
</Thumb.Template>
</Thumb>
</Grid>
</Grid>
</ControlTemplate>
And I have tried a corresponding Style. What could be the problem? Should I use Style or Template? Both acts equally for me now..
Had the same issue. Before WP8, we had edited a copy of its original style and modified it to our purposes. Later, when upgrading the project to WP8 this modified style from WP7 caused the slider not getting updated anymore as the style has changed since. Removing the style fixed it. In case the modifications are still needed, you should get the original style from WP8's slider component.

ScrollViewer in Windows 8: always show vertical scrollbar

How always show vertical scrollbar in the scrollviewer?
It disappear in few seconds, but I want to make scroll visible all the time when scrolling is available
Thanks for any help
I think there might be a bug in the control in Windows 8 Consumer Preview, since the following should normally work:
<ScrollViewer
Style="{StaticResource VerticalScrollViewerStyle}"
VerticalScrollBarVisibility="Visible"
Template="{StaticResource ScrollViewerControlTemplate1}">
As a workaround you can modify the template of the ScrollViewer:
<ScrollViewer
Style="{StaticResource VerticalScrollViewerStyle}"
Template="{StaticResource ScrollViewerControlTemplate1}">
... elsewhere in some ResourceDictionary - the modified standard ScrollViewer template with the "NoIndicator" VisualState removed.
<ControlTemplate
x:Key="ScrollViewerControlTemplate1"
TargetType="ScrollViewer">
<Border
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
x:Name="ScrollingIndicatorStates">
<VisualState
x:Name="TouchIndicator">
<Storyboard>
<FadeOutThemeAnimation
TargetName="ScrollBarSeparator" />
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="VerticalScrollBar"
Storyboard.TargetProperty="IndicatorMode"
Duration="0">
<DiscreteObjectKeyFrame
KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<ScrollingIndicatorMode>TouchIndicator</ScrollingIndicatorMode>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="HorizontalScrollBar"
Storyboard.TargetProperty="IndicatorMode"
Duration="0">
<DiscreteObjectKeyFrame
KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<ScrollingIndicatorMode>TouchIndicator</ScrollingIndicatorMode>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState
x:Name="MouseIndicator">
<Storyboard>
<FadeInThemeAnimation
TargetName="ScrollBarSeparator" />
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="VerticalScrollBar"
Storyboard.TargetProperty="IndicatorMode"
Duration="0">
<DiscreteObjectKeyFrame
KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<ScrollingIndicatorMode>MouseIndicator</ScrollingIndicatorMode>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="HorizontalScrollBar"
Storyboard.TargetProperty="IndicatorMode"
Duration="0">
<DiscreteObjectKeyFrame
KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<ScrollingIndicatorMode>MouseIndicator</ScrollingIndicatorMode>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid
Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="*" />
<ColumnDefinition
Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition
Height="*" />
<RowDefinition
Height="Auto" />
</Grid.RowDefinitions>
<ScrollContentPresenter
x:Name="ScrollContentPresenter"
Grid.RowSpan="2"
Grid.ColumnSpan="2"
ContentTemplate="{TemplateBinding ContentTemplate}"
Margin="{TemplateBinding Padding}" />
<ScrollBar
x:Name="VerticalScrollBar"
Grid.Column="1"
IsTabStop="False"
Maximum="{TemplateBinding ScrollableHeight}"
Margin="1,0,0,0"
Orientation="Vertical"
Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
Value="{TemplateBinding VerticalOffset}"
ViewportSize="{TemplateBinding ViewportHeight}"
HorizontalAlignment="Right" />
<ScrollBar
x:Name="HorizontalScrollBar"
IsTabStop="False"
Maximum="{TemplateBinding ScrollableWidth}"
Margin="0,1,0,0"
Orientation="Horizontal"
Grid.Row="1"
Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
Value="{TemplateBinding HorizontalOffset}"
ViewportSize="{TemplateBinding ViewportWidth}" />
<Rectangle
x:Name="ScrollBarSeparator"
Grid.Row="1"
Grid.Column="1"
Margin="1,1,0,0"
StrokeThickness="1"
Fill="{StaticResource ScrollBarTrackBrush}"
Stroke="{StaticResource ScrollBarTrackBorderBrush}" />
</Grid>
</Border>
</ControlTemplate>
In Blend:
You can make it visible by editing the template of the VerticalScrollBar which is a part of the ScrollViewers template, which is itself a part of the ListBox template.
Set the Visibility of the VerticalScrollBar to Visible, set it's opacity to 100% then it will be permanently visible.
I have only developed store apps in HTML/CSS/JavaScript and I experienced the same problem there.
I wanted the scroll bars to always show even when moving the cursor away.
I found this solution in CSS:
div#overflowableDiv{
overflow-y: auto;
-ms-overflow-style: scrollbar;
}
See: http://msdn.microsoft.com/en-us/library/windows/apps/hh441298.aspx

Change ChildWindow CloseButton image Silverlight 3

I'm modifying the default 'ChildWindow' style and I'd like to specify a different image than the 'X' that is there by default. I've tried various things, like hooking into the OnApplyTemplate and OnOpened, where I can gain programmatic access to the button, like this:
Button closeButton = this.GetTemplateChild("CloseButton") as Button;
Yet closeButton.Content is always null. I've tried setting this to my Image and it does get set, yet the UI still displays the default 'X'. I've called UpdateLayout() as well to no avail.
Is there a way to do this either programmatically or via XAML? I've copied the default style and have made changes that are affected, yet this is one change that has me stumped. Below is a snippet of the style XAML I've been working with:
<!-- Header with Title-->
<Border x:Name="Chrome" Width="Auto" CornerRadius="5,5,0,0" Background="{StaticResource ChildWindowHeaderBackgroundBrush}">
<Grid Height="Auto" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<ContentControl Content="{TemplateBinding Title}"
IsTabStop="False"
FontWeight="Bold"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Margin="6,0,6,0"/>
<Button x:Name="CloseButton" Margin="0,5,0,5" Grid.Column="2" IsTabStop="False" HorizontalAlignment="Center" VerticalAlignment="Center" Width="15" Height="14" Style="{StaticResource ButtonStyle}"/>
</Grid>
</Border>
I've added an Image to the Button via XAML and it still doesn't show up.
It's all handled via the style, in a ControlTemplate:
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Disabled"/>
<VisualState x:Name="Normal"/>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="mouseOverImage">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="normalImage">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image x:Name="normalImage" Margin="0" Source="/img/status_unknown.png" Stretch="None" />
<Image x:Name="mouseOverImage" Margin="0" Source="/img/up.png" Stretch="None" Visibility="Collapsed" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>