I have a grid of 12 non clickable buttons(they are just indicators) in the center of my page in the following manner.
There is going to be more stuff around this grid and I need the items to be at their exact place and of the same width and height. I am confused whether to use a grid with (star sized) rows and columns with buttons in them or a gridview with a wrap panel with MaxRowsAndColumns set to 3 and Orientation set to Horizontal. Here are my arguments for and against each of the approaches:-
Buttons:-
Pros:-
Fixed position and height and width guaranteed.
Adaptable to all screen sizes
Easy to code
Cons:-
Will have to write a very large XAML and C# code because I will not be able to use Data Template here and will have to define lots of variables and grids and set values of each textblock separately.
GridView
Pros:-
DataTemplate available so easy to set values via ObservableCollection
Smaller code than the one created using buttons
Cons:-
No guarantees of item size. Will have to hard code it in XAML or Bind it to properties in Model and then calculate the value inside XAML.
Please let me know which one is the better alternative.
Is there any other easier way to do it apart from the methods mentioned above?
Thanks,
Rajeev
Use ItemsControl, it offers DataTemplate as well as item size.
Out of the two above I would say GridView. Since you need to support so many different screen sizes and ratios you shouldn't have fixed sizes.
As the guidelines states: Guidelines for window sizes and scaling to screens(Windows Store apps)
Design apps that look good at any width, and you automatically get
support for different screen sizes and orientations. Plan your app for
resizing from full screen down to the minimum width so that the UI
reflows gracefully for various screen sizes, window sizes, and
orientations.
Since you also need to cater for the snapped mode (which you can't choose not to have in you application) having buttons will add even more manual work which means a lot of hard to maintain UI code. If you use a gridview you can pair it up with a listview for the snapped mode (gridview for full mode, listview for snapped) as in the templates.
You can certainly restrict the sizes in a GridView without it require even a fifth of the work needed for buttons, so I'm not quite sure what you mean by "No guarantees of item size".
Anyway, as the guidelines says, buttons should NOT be used for navigation to a page. These are just guidelines, but I reckon they make sense.
Here are: Guidelines and checklist for buttons (Windows Store apps)
Don't use a button when the action is to navigate to another page; use
a link instead. Exception: For wizard navigation, use buttons labeled
"Back" and "Next".
I would use GriView and combine it with a semantic zoom if appropriate.
Example with GridView and ItemsControl, here is the result:
Code for the UI (View):
<Page
x:Class="App1.MainPage"
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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<GridView x:Name="gridView">
<GridView.ItemTemplate>
<DataTemplate>
<Border Width="150" Height="150" BorderBrush="Pink" BorderThickness="10" Background="Aqua">
<TextBlock Foreground="Black" FontSize="20" Text="{Binding}"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
<ItemsControl Grid.Row="1" x:Name="itemscontrol">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="150" Height="150" BorderBrush="Yellow" BorderThickness="10" Background="LightGreen">
<TextBlock Foreground="Black" FontSize="20" Text="{Binding}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Page>
Code for the codebehind (the .cs file that belongs to the XAML):
using System.Collections.Generic;
namespace App1
{
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
DataContext = this;
var items = new List<string> { "Iris", "Paul", "Ben", "Cate", "Daniel", "Ryan", "Iris 2", "Paul 2", "Ben 2", "Cate 2", "Daniel 2", "Ryan 2" };
gridView.ItemsSource = items;
itemscontrol.ItemsSource = items;
}
}
}
The result at a higher resolution, notice that the items keep their fixed size and don't scale to fit the screen.
Stretching the items height over one row with the GridView:
<GridView x:Name="gridView">
<GridView.ItemTemplate>
<DataTemplate>
<Border Width="150" BorderBrush="Pink" BorderThickness="10" Background="Aqua">
<TextBlock Foreground="Black" FontSize="20" Text="{Binding}"/>
</Border>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</GridView.ItemContainerStyle>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
By the way, GridView inherits from the ItemsControl if you didn't know.
Related
I'm trying to create a UWP calculator app and for the display I have a slight issue. The calculator is the type that you input the text and eventually click enter and it respects orders of operation. As such, the input may get quite long so I'll need a scrollviewer around a textbox. I'm working on Windows 10 Creator's Update but the app should be backwards compatible to release version if possible.
I intend on using buttons to control cursor position, but I'd still like to have a scrollbar to indicate where you are, horizontally, in the long string. I can't figure out how to make the scrollbar there only as a visual indicator, not as a way of controlling the scrollviewer. When the mouse cursor goes near it the scrollbar expands and allows user input; this I want to avoid.
This is my display currently, very basic. How would I go about modifying that scrollviewer to my purposes?
<Border>
<ScrollViewer HorizontalScrollMode="Auto" VerticalScrollMode="Disabled" HorizontalScrollBarVisibility="Auto">
<TextBox IsReadOnly="True" Margin="5" FontSize="24" Text="long string of testing text"/>
</ScrollViewer>
</Border>
Disable hit testing on the ScrollBar:
<Border>
<ScrollViewer HorizontalScrollMode="Auto"
VerticalScrollMode="Disabled"
HorizontalScrollBarVisibility="Auto">
<ScrollViewer.Resources>
<Style TargetType="ScrollBar">
<Setter Property="IsHitTestVisible"
Value="False"/>
</Style>
</ScrollViewer.Resources>
<TextBox IsReadOnly="True"
Margin="5"
FontSize="24"
Text="long string of testing text" />
</ScrollViewer>
</Border>
I try to populate radio buttons dynamically in Universal windows application. I already wrote VisualState for different
of screens. Now I try to populate the radio button, which are they have to take the whole width of the window. I able to set fixed width for every VisualState But I think that may not good practice and difficult to handle further.
<GridView Grid.Row="1" Height="auto" Width="auto" HorizontalAlignment="Stretch" ItemsSource="{Binding DamageLocationList}">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate x:DataType="model:DamageLocations">
<Grid>
<RadioButton Style="{StaticResource ButtonRadioButtonStyle}" HorizontalContentAlignment="Center" HorizontalAlignment="Stretch" IsChecked="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" Content="{Binding DamageLocation}" Margin="0" Click="RadioButton_Checked" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
As #Ashok Rathod said, you can try using UniformGrid as the ItemsPanel of your Grid to make your radio buttons take the whole width of your app's window.
Although UniformGrid is not exist in UWP. But we can implement it by ourselves or use a third party UniformGrid like what in WinRTXamlToolkit.
Using WinRTXamlToolkit for example, we can using
<toolkit:UniformGrid Rows="1" />
instead of
<VariableSizedWrapGrid Orientation="Horizontal" />
Here toolkit is the namespace of WinRTXamlToolkit.Controls:
xmlns:toolkit="using:WinRTXamlToolkit.Controls"
As I didn't set Columns property, it will be the default value which is 0. A value of zero (0) for the Columns property specifies that the column count is computed based on the number of rows and the number of visible child elements that are in the Grid. Since I set Rows to 1, all the items will be put in one row and have the same width.
After this, you may also need to set ItemContainerStyle to make the radio buttons stretch like:
<GridView.ItemContainerStyle>
<Style TargetType="GridViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</GridView.ItemContainerStyle>
Got a GridView that its items width are set by the first one width(Screenshot)
How could I set some auto width for each item?
<GridView ItemsSource="{x:Bind PopularItems}" Style="{StaticResource GVStyle}" Height="230">
<GridView.ItemTemplate>
<DataTemplate x:Name="GVTemp" x:DataType="models:LessDetails">
<StackPanel>
<Image Height="180" Width="132" Source="{x:Bind Img}" />
<TextBlock Margin="4,4,0,4" HorizontalAlignment="Stretch" TextAlignment="Center"
Text="{x:Bind Name}" Style="{ThemeResource BodyTextBlockStyle}" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
You can not auto-width each item in a GridView control, it is by designed to use the first item's size as the uniform size (ListViewItemPresenter for each items has the same size).
For the auto-size scenario, I suggest you using the StackPanel or creating a custom ItemsControl.
Use an ItemsStackPanel:
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
The only downside is that it doesn't wrap items.. For that you can use the ItemsWrapGrid but then you lose the auto-width functionality (first item determines the width of all items).
There's also the VariableSizedWrapGrid that can be used by giving items a certain ColumnSpan/RowSpan, but it's not quite the same as automatically calculating the width.
If you want the best of both world, probably you'll need to write your own custom panel.
UPDATE: For a solution that allows for both automatic width and item wrapping, you can use the WrapPanel control from the UWP Community Toolkit.
I'm building a universal app and my Win8.1 app has to show a grid of items. Normally the grid consists of 3 columns, but on smaller screens I want the items to wrap so that there are only 2 columns (or even 1). This is my code:
<GridView ItemsSource="{Binding Regions}" IsItemClickEnabled="True">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3" MinWidth="400" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10">
<Image Source="{Binding Code, Converter={StaticResource FlagIconConverter}, ConverterParameter='/Assets/Icons/Flags/{0}.png'}" Width="30" />
<TextBlock Text="{Binding NativeName}" Style="{StaticResource BodyTextBlockStyle}" Margin="10,0,0,10" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Whenever I make the app smaller, the items do not automatically wrap. I tried solving this by changing the MaximumRowsOrColumns property with a VisualStateManager but that didn't work because it couldn't access my WrapGrid for some reason. Changing the property from code-behind didn't work either, because again it couldn't access the WrapGrid.
I tried this with both WrapGrid and ItemsWrapGrid (what is the difference anyway?) and ListView and GridView. No difference there.
Does anyone know how to accomplish this?
You shouldn't need to do anything. It should wrap based on the available client area. The only thing I can think of that would not make it wrap is that you put your <GridView> inside a fixed width container or a container that is size Auto in which you don't update the Observable Collection to notify the Grid to redraw/update itself.
For example this will not wrap.
<Grid Width="1000">
<GridView x:Name="myGridView" IsItemClickEnabled="True">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="5"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<!-- DATATEMPLATE -->
</GridView.ItemTemplate>
</GridView>
</Grid>
However get rid of that Width=1000 and it will wrap.
Example output 3 different sizes
I've created a simple user control for my xaml project, but as you can see from my image i cant seem to be able to do certain things.
Ignore the red line, its the size of the control for illustrate its size.
It's placement should be middle of the screen:
<Client:TileMenu HorizontalAlignment="Center" VerticalAlignment="Center" Name="TileOverlayMenu" Background="Azure" BorderBrush="Aquamarine" BorderThickness="3" />
And as you see its background color should be "Azure" with a blueish border of 3.
Why is this?
In the background I have a Canvas:
<Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0">
<Canvas Name="GameCanvas">
<Canvas.RenderTransform>
<CompositeTransform x:Name="CanvasRenderTransform" />
</Canvas.RenderTransform>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener DragStarted="GestureListener_DragStarted" DragDelta="GestureListener_DragDelta" Tap="GestureListener_Tap" PinchStarted="GestureListener_PinchStarted" PinchDelta="GestureListener_PinchDelta"/>
</toolkit:GestureService.GestureListener>
</Canvas>
<Client:TileMenu HorizontalAlignment="Center" VerticalAlignment="Center" Name="TileOverlayMenu" Background="Azure" BorderBrush="Aquamarine" BorderThickness="3" />
</Grid>
As for my third problem, having the events in the canvas causes the Move slider to be interrupted, making me only able to push it a little each time :-/
In case TileMenu is a UserControl you would have to set these properties on the top level container in the UserControl's XAML as this defines the entire visual structure of the control.
You could bind to the appropriate values in the UserControl, however:
<UserControl x:Class="YourNamespace.TileMenu" ...
x:Name="tileMenu">
<Border BorderBrush="{Binding BorderBrush, ElementName=tileMenu}"
BorderThickness="{Binding BorderThickness, ElementName=tileMenu}">
<Grid>
...
</Grid>
</Border>
</UserControl>