Win10 Xaml: How to get scale factor of viewbox? - winrt-xaml

I have some problem: need to make animation that starts from element inside viewbox and finishes outside viewbox
It means:
I have page where elements are placed. There is text block on the top of it and viewbox. I place gridview into this viewbox. And one moment it should start animation from gridview item
<Viewbox HorizontalAlignment="Left" Stretch="Uniform" VerticalAlignment="Top" x:Name="vbChallengeView" >
<Grid x:Name="gdChallenges">
<GridView
Margin="92,64,68,0"
x:Name="GvChallenges"
ItemsSource="{Binding Source={StaticResource GroupedItemsViewSource}}"
VerticalAlignment="Top"
HorizontalAlignment="Left"
ItemTemplate="{StaticResource ChallengeTileTemplate}"
ItemClick="GvPack_OnItemClick"
IsItemClickEnabled="True"
IsZoomedInView="False"
SelectionMode="None"
>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Margin" Value="0,0,24,24" />
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid MaximumRowsOrColumns="5" Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
</Grid>
</Viewbox>
How can I determine item size in view box and it's position relatively current window?
Thanks for any ideas

Related

Width CommandBar (UWP)

how can I change the width of CommandBar? I want the size of Desktop mode on Mobile mode.
Desktop mode:
image
Mobile mode: image
Edit:
This is my code, I don't use Flyout.
<CommandBar RelativePanel.AlignRightWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True" Margin="0">
<CommandBar.SecondaryCommands>
<AppBarButton Name="shareButton" Label="Condividi" x:Uid="condividi" Click="shareButton_Click" Icon="ReShare" Style="{StaticResource AppBarButtonStyle1}" />
<AppBarButton Name="contactButton" Icon="Contact" x:Uid="contatti" Label="Contatti" Click="contactButton_Click" Style="{StaticResource AppBarButtonStyle1}" />
</CommandBar.SecondaryCommands>
the problem is not the commandbar. you need to check your xaml in the Flyout set the horizontal alignment to right of your items inside of the Flyout
The default overflow menu is styled to be distinct from the bar. You can adjust the styling by setting the CommandBarOverflowPresenterStyle property to a Style that targets the CommandBarOverflowPresenter. By default the overflow menu adapts its size and visuals based on the window width so that on small windows/screens it stretches to be the full width and only shows the border along the leading edge instead of around the entire menu. You can override this by re-templating the menu. In the example below I'm removing some of the visual states that alter the BorderThickness and I'm hard-coding the MaxWidth / MinWidth as well as the HorizontalAlignment (to prevent it from being stretched on smaller windows).
<CommandBar Margin="0">
<CommandBar.CommandBarOverflowPresenterStyle>
<Style TargetType="CommandBarOverflowPresenter">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CommandBarOverflowPresenter">
<Grid x:Name="LayoutRoot"
MaxWidth="480"
MinWidth="196"
HorizontalAlignment="Right"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1,1,1,1">
<ScrollViewer HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
ZoomMode="{TemplateBinding ScrollViewer.ZoomMode}"
AutomationProperties.AccessibilityView="Raw">
<ItemsPresenter x:Name="ItemsPresenter" Margin="0,7,0,7" />
</ScrollViewer>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</CommandBar.CommandBarOverflowPresenterStyle>
<CommandBar.SecondaryCommands>
<AppBarButton Name="shareButton" Label="Condividi" x:Uid="condividi" Icon="ReShare"/>
<AppBarButton Name="contactButton" Icon="Contact" x:Uid="contatti" Label="Contatti"/>
</CommandBar.SecondaryCommands>
</CommandBar>

Putting an ItemContainerStyle on a Grid

Obviously ItemContainerStyle doesn't exist in a Grid, which is the problem.
My program has a ListView that displays a bunch of projects. I want to add a button to each project that gives more info, and that much I can do easily. The problem I'm encountering is the border that appears on each project, which is toggled on/off by clicking the project to show the user which are currently selected. I don't want this border to encircle my button; I want the button placed to the right of the bit with the border.
I can't put the button on a separate list or I'll get two different scroll-able lists, so it must stay within the ListView, but any border on the ListView will encircle everything in the ListView. My solution was to make a StackPanel with two Grids inside the ListView, where the first Grid has all of my old stuff and the toggle-able border and the second grid only has my button. So now I just need to move that border onto the Grid... but how?
XAML code:
<!-- Other code up above... -->
<ListView x:Name="lstAvailableProjects"
Grid.Row="1"
ItemsSource="{Binding ProjectList, Mode=TwoWay}"
Height="Auto"
Width="Auto"
SelectionMode="Multiple"
IsRightTapEnabled="True"
IsDoubleTapEnabled="False"
IsSwipeEnabled="False"
SelectionChanged="lstAvailableProjects_SelectionChanged"
Background="Transparent"
VerticalAlignment="Top"
ItemContainerStyle="{StaticResource ActiveProjectListViewStyle}"
BorderThickness="30,0,0,0">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Grid HorizontalAlignment="Stretch" Width="250" Background="{Binding Converter={StaticResource projectStateColorConverter}}">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding ProjectName}"
Foreground="Black"
Margin="10 5 0 0"
FontSize="17"
VerticalAlignment="Center"
Grid.Row="0"
HorizontalAlignment="Stretch"/>
<TextBlock Text="{Binding AssignmentRole}"
Visibility="{Binding ProjectAssignmentRole, Mode=OneWay, Converter={StaticResource visibilityConverter}}"
Foreground="Black"
Margin="10 3 0 5"
FontSize="14"
Grid.Row="1"
HorizontalAlignment="Stretch"/>
<TextBlock Text="{Binding StatusText}"
Foreground="Red"
Margin="10 3 0 5"
Grid.Row="2"
FontSize="14"/>
</Grid>
<Grid>
<Button>
<Button.Template>
<ControlTemplate>
<Image Source="../Assets/setting_icon.png" Height="40" />
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Since the grid items are wrapped You cannot make changes in xaml to achieve it other than overriding the preparecontainerforitemoverride.
A little tricky hack would be to apply transform to the button.
<Button.RenderTransform>
<CompositeTransform TranslateX="100" TranslateY="100"/>
<Button.RenderTransform>

WinRT XAML Toolkit TreeView expanded display

I am developing a Windows store app. At a high level, the page has a Grid with two rows. Along with other controls, the ComboBox is placed in the first row.
The second row has a GridView and the red tile is one of the items in the GridView. The ComboBox is used to display hierarchical data as shown here.
ComboBox
I am working on replacing the ComboBox with a TreeView from WinRT XAML Toolkit as shown here.
TreeView
What I like about the Combobox is that when its open, the opened list sits on top of the GridView. For the Treeview, when I open the parent node, the opened list stays within the specified height along with the scrollbar.
I would like the TreeView to act like the ComboBox so that when opened, it extends out and the opened list sits on top of the GridView. Any idea how I can accomplish that?
Thanks for your help.
Set something like this on the TreeView (adjust max dimensions for available space)
VerticalAlignment="Top"
HorizontalAlignment="Left"
MaxHeight="500"
MaxWidth="500"
Canvas.ZIndex="1"
This will make the ScrollViewer and the TreeView change the size depending on the size of expanded tree up to a max size where it will start scrolling. The ZIndex will make it render on top of items with lower or default ZIndex value at the same level in the visual tree.
You could also put it in a Popup when your root node expands and back out of the Popup when the Popup closes or when the node collapses.
*EDIT
Based on your code - I can see you have a bit of a mess of a lot of nested panels, but with a simple hack you can make it work even with your layout. There are three things to do:
Make the TreeView top-aligned as I mentioned earlier.
The height of the panel the TreeView is inside of is a bit limited (to 20% of page height) by the size of the Grid row it's in. You can add a bit more space by setting a large enough negative bottom Margin value for the TreeView.
Since the TreeView is in the first row of the grid - the second row naturally overlays it when the TreeView extends into the negative margin. You can fix that by setting Canvas.ZIndex="1" on the StackPanel that is the root of your first row or reordering your row elements to reverse the ZIndex.
Here's the updated code:
<Page
x:Class="TestApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestApp"
xmlns:custom="using:TestApp.Custom"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:WinRTXamlToolkit.Controls"
xmlns:data="using:WinRTXamlToolkit.Controls.Data"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="HorizontalScrollViewerStyle" TargetType="ScrollViewer">
<Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="VerticalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.HorizontalScrollMode" Value="Enabled" />
<Setter Property="ScrollViewer.VerticalScrollMode" Value="Disabled" />
<Setter Property="ScrollViewer.ZoomMode" Value="Disabled" />
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="2*"></RowDefinition>
<RowDefinition Height="7*"></RowDefinition>
<RowDefinition Height="1*"></RowDefinition>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Margin="0,10,0,0"
Canvas.ZIndex="1">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" HorizontalAlignment="Left" Margin="50,0,0,0">
<StackPanel>
<TextBlock Text="Version 1.0.0.0"/>
<!--<ProgressBar IsIndeterminate="True" Visibility="{Binding ShowProgressBar, Converter={StaticResource BooleanToVisibility}}" Height="20"/>-->
</StackPanel>
<StackPanel Orientation="Horizontal">
<Image Source="/Assets/SmallLogo.scale-100.png" Width="130" Height="60" Stretch="Uniform"/>
<TextBlock Text="Testing TreeView" FontSize="20" FontWeight="Light" VerticalAlignment="Center" Margin="10"/>
</StackPanel>
</StackPanel>
<StackPanel Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal">
<TextBlock Text="Test ID: 1234" Margin="0,0,5,0" FontWeight="SemiBold" VerticalAlignment="Center"/>
</StackPanel>
</Grid>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text ="Test Data 123"/>
<TextBlock Text ="More Test Data 123"/>
</StackPanel>
<!--<ComboBox Grid.Row="2"
ItemsSource="{Binding Locations}"
SelectedItem="{Binding SelectedLocation, Mode=TwoWay}"
Width="400" Margin="0,5,0,0" />-->
<controls:TreeView Grid.Row="2" ItemsSource="{Binding TLocations}"
VerticalAlignment="Top"
Width="400" Margin="0,5,0,-1000" MaxHeight="400">
<controls:TreeView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>
<data:DataTemplateExtensions.Hierarchy>
<data:HierarchicalDataTemplate ItemsSource="{Binding Items}" />
</data:DataTemplateExtensions.Hierarchy>
</DataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
</Grid>
</StackPanel>
</Grid>
</StackPanel>
<Grid Grid.Row="1" Margin="0,10,0,0">
<!--<Grid.Background>
<ImageBrush ImageSource="/Assets/Background.png" />
</Grid.Background>-->
<ScrollViewer Style="{StaticResource HorizontalScrollViewerStyle}">
<StackPanel Orientation="Horizontal" >
<StackPanel Margin="50,0,0,0">
<TextBlock Text="Overview" Margin="0,20,0,0" />
<Grid Width="450" Height="450" HorizontalAlignment="Left" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition Height="5*" />
<RowDefinition Height="2*" />
<RowDefinition Height="3*" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="Assets/StoreLogo.scale-100.png" Stretch="UniformToFill"/>
<StackPanel Grid.Row="1" Background="Azure">
</StackPanel>
<StackPanel Grid.Row="2" Background="Orange">
</StackPanel>
</Grid>
</StackPanel>
<StackPanel Margin="50,20,0,0">
<TextBlock Text="Test Content" Margin="2,0,0,0"/>
<custom:CustomGridView ItemsSource="{Binding Items}" SelectionMode="None">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Horizontal" ItemHeight="150" ItemWidth="175" MaximumRowsOrColumns="3" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Background="{Binding BackgroundColor}">
<Grid.RowDefinitions>
<RowDefinition Height="{Binding TitleHeight}"/>
<RowDefinition Height="{Binding ImageHeight}" />
<RowDefinition Height="{Binding ContentHeight}" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" VerticalAlignment="Top">
<TextBlock Text="{Binding Title}" HorizontalAlignment="Left" Padding="5,0,0,0"></TextBlock>
</StackPanel>
<StackPanel Grid.Row="1">
<Image Source="{Binding ImageSource}" Stretch="None" VerticalAlignment="Center" />
</StackPanel>
<StackPanel Grid.Row="2" VerticalAlignment="Bottom" Background="{Binding ContentBackgroundColor}">
<TextBlock Text="{Binding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="5"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</custom:CustomGridView>
</StackPanel>
<!--<userControls:EarnBenefits x:Name="earnBenefits"/>-->
</StackPanel>
</ScrollViewer>
</Grid>
</Grid>
</Page>
Here's a screenshot:
I've also noticed you are targeting Windows 8.1 at least with your test app, but you are using an old Windows 8.0 version (1.6.1.3) of the toolkit. You can get the latest version (1.8.1 as of 2015-02-01) here: http://www.nuget.org/packages/winrtxamltoolkit.windows

Layouting inside DockPanel

Hi there I am trying to layout may App in dockpanel.
I have on relatively wider Grid on left and narrow Grid on right. I want the right grid of fixed width but left one to grow as I resize my main window. Here is my XAML:
<Window
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"
x:Class="WebSpark.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">
<DockPanel x:Name="LayoutRoot" Background="#FF474747" LastChildFill="True">
<Grid DockPanel.Dock="Top" Height="23" Background="#FFEF1212" />
<Grid DockPanel.Dock="Top" Height="23" Background="#FFF7E30D" />
<Grid DockPanel.Dock="Left" Background="#FF0A38F1" MinHeight="396" Width="{Binding ElementName=LayoutRoot,Path=ActualWidth}" MaxWidth="428" />
<Grid DockPanel.Dock="Right" HorizontalAlignment="Right" Width="198" />
</DockPanel>
Here is the behavior. Restored window: http://s17.postimage.org/tk1pov6b3/Restored.png
Maximized window: http://s9.postimage.org/457s5r23j/Maximized.png
As you can see in the maximized window, the blue panel should have stretched, but it is not. What's wrong here? Please help. Thank you.
Take advantage of LastChildFill="True". This code ensures that the grey box has fixed size and the blue one has a variable size.
<DockPanel x:Name="LayoutRoot" Background="#FF474747" LastChildFill="True">
<Grid DockPanel.Dock="Top" Height="23" Background="#FFEF1212" />
<Grid DockPanel.Dock="Top" Height="23" Background="#FFF7E30D" />
<Grid DockPanel.Dock="Right" HorizontalAlignment="Right" Width="198" />
<Grid DockPanel.Dock="Left" Background="#FF0A38F1" MinHeight="396" />
</DockPanel>

Center a TextBlock inside a ListBox.ItemTemplate

I'm developing a Windows Phone app.
I have the following XAML code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="GameList" Margin="12" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10,10,10,5" Height="67" HorizontalAlignment="Center" VerticalAlignment="Center" >
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
But I couldn't set textblock centered (vertically and horizontally).
There are two ways to achieve this.
The first solution is to specify the ListBox's ItemContainerStyle inside the ListBox and set the HorizontalContentAlignment property to Center.
<ListBox ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Collection}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
The second solution is to define a style, and apply the style to the ListBox (so it's reusable).
<Style x:Key="ListBoxCenteredItemStyle" TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
<ListBox
ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Collection}"
ItemContainerStyle="{StaticResource ListBoxCenteredItemStyle}"/>
The ItemTemplate of a ListBox is just a DataTemplate for displaying each data item. If there is a need to style a single row, the ItemContainerStyle is the guy. :)

Resources