"How can I center a navigation view item in the UWP NavigationView control when the pane display mode is set to 'Top'?" - xaml

I am trying to create a navigation menu using the NavigationView control in UWP, and I want to center a navigation item when the pane display mode is set to 'Top'.
I have tried using the HorizontalAlignment and VerticalAlignment properties, but they don't seem to have any effect. Can anyone provide an example of how to center a navigation item in NavigationView control when the pane display mode is set to 'Top'?
<Grid>
<NavigationView PaneDisplayMode="Top"
HorizontalContentAlignment="Center">
<NavigationView.MenuItems>
<NavigationViewItem Content="Nav iTem"/>
<NavigationViewItem Content="Nav iTem"/>
<NavigationViewItem Content="Nav iTem"/>
</NavigationView.MenuItems>
</NavigationView>
</Grid>

You will need to modify the default style of the NavigationView to achieve what you want. You could refer to #FrozenAssassine's answer here: https://stackoverflow.com/a/74861708/8443430 to create a copy of the default style of the NavigationView. Then search for x:Name="TopNavGrid" and you will see the part of navigation items when in the top mode.
Part of the default style:
<Grid x:Name="TopNavGrid" .....>
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="BackButtonPlaceholderOnTopNav" Width="{ThemeResource NavigationBackButtonWidth}"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="48" Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
... other controls..
<NavigationViewList x:Name="TopNavMenuItemsHost"
Grid.Column="3"
HorizontalContentAlignment="Center"
.... other properties...
>
</NavigationViewList>
The items are in a NavigationViewList control called TopNavMenuItemsHost and the TopNavMenuItemsHost is placed in a column of the TopNavGrid. The width of the column is set to Auto which means the width depends on the content. The column will be as big as the TopNavMenuItemsHost. So you can't make the items placed in the center.
To change the beahvior, please modify the ColumnDefinitions to the following:
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="BackButtonPlaceholderOnTopNav" Width="{ThemeResource NavigationBackButtonWidth}"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition MinWidth="48" Width="48"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
Make the ColumnDefinition of the TopNavMenuItemsHost bigger and then you will be able to set the HorizontalAlignment="Center" to the TopNavMenuItemsHost.
<NavigationViewList x:Name="TopNavMenuItemsHost"
Grid.Column="3"
.... other properties...
HorizontalAlignment="Center"
SingleSelectionFollowsFocus="{Binding TemplateSettings.SingleSelectionFollowsFocus, RelativeSource={RelativeSource Mode=TemplatedParent}}"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollMode="Disabled">

Related

UWP app DropDownButton cannot be added to IGridList

I am trying to create a Flyout menu for each grid cell that opens via dropdown button. However, whatever I try, I cannot get it to work or even build I should say.
My code is
<DataTemplate>
<ViewCell>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Label Grid.Column="2" Grid.Row="0" Text="1" />
<Label Grid.Column="3" Grid.Row="0" Text="2" />
<Label Grid.Column="4" Grid.Row="0" Text="3" HorizontalOptions="Start" />
<controls:DropDownButton Grid.Column="4" Grid.Row="0" HorizontalOptions="End"></controls:DropDownButton>
but I get the error
XLS0503 A value of type 'DropDownButton' cannot be added to a collection or dictionary of type 'IGridList1'
Is there some parent element I need to add before?
UWP application doesn't have ViewCell element.
Look for all available GUI and Layout elements in WinUI3 or WinUI2
Read more about WinUI2 here

Why I cannot set canvas's width via binding method?

My UI elements' layout (nest relation) is:
toolbarGrid -> toolbarCanvas -> toolbarPanel -> some buttons
My purpose is to make animation of those buttons.
The issue is that I use Debug.Write() to monitor the Width and ActualWidth parameters of the UI elements. And I found that when App screen size is changed, only toolbarGrid size is changed accordingly. While the other UI elements' sizes are not changed at all, although I use Binding method to bind their Width and ActualWidth to toolbarGrid.
So how can I set their Width and ActualWidth so that they could change according to App screen's real size?
toolbarPanel width = 1920, actualWidth = 1920
toolbarGrid width = NaN, actualWidth = 1034
toolbarBackground width = 1920, actualWidth = 1920
toolbarCanvas width = 1920, actualWidth = 1920
<Grid x:Name="gridTotal">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle x:Name="recTangle1" Grid.Column="0" Grid.Row="0"/>
<Rectangle x:Name="recTangle2" Grid.Column="0" Grid.Row="8" Grid.ColumnSpan="12"/>
<Grid x:Name="toolbarGrid" Grid.Column="0" Grid.Row="8" Grid.ColumnSpan="12">
<Canvas x:Name="toolbarCanvas" Width="{Binding ElementName=toolbarGrid, Path=ActualWidth}">
<RelativePanel x:Name="toolbarPanel" Canvas.Top="0" Width="{Binding ElementName=toolbarGrid, Path=ActualWidth}">
<Image x:Name="toolbarBackground" Source="ms-appx:///Assets/MainPage/toolbar/toolbar-background2.png"
Width="{Binding ElementName=toolbarGrid, Path=ActualWidth}"
Height="{Binding ElementName=toolbarGrid, Path=ActualHeight}" Stretch="Fill" />
<Viewbox x:Name="vb2" Stretch="Uniform" RelativePanel.AlignHorizontalCenterWithPanel="True" Margin="20,0,20,0"
Height="{Binding ElementName=recTangle1, Path=ActualHeight}"
Width="{Binding ElementName=recTangle1, Path=ActualWidth}">
<Button x:Name="toolbarHistory" Style="{StaticResource toolbarImageStyle}">
<Image Source="ms-appx:///Assets/MainPage/toolbar/toolbar-history.png" Stretch="UniformToFill" />
</Button>
</Viewbox>
</RelativePanel>
</Canvas>
</Grid>
</Grid>
I tried to understand your problem. If the problem was that u like to Elements follow actual screen size this one works. I changed the XAML code and hoping that next solution works fine for you. I rid of the Canvas because that element cause the problem. Anyway, in this example u do not need it (unless u have planned some other use later). And also to set Row/Column Width/Height values is unneeded. Bindings are also unnessessary.
<Grid x:Name="gridTotal">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Rectangle x:Name="recTangle1" Grid.Column="0" Grid.Row="0"/>
<Rectangle x:Name="recTangle2" Grid.Column="0" Grid.Row="8" Grid.ColumnSpan="12"/>
<Grid x:Name="toolbarGrid" Grid.Column="0" Grid.Row="8" Grid.ColumnSpan="12">
<RelativePanel x:Name="toolbarPanel" Canvas.Top="0">
<Image x:Name="toolbarBackground" Source="ms-appx:///Assets/MainPage/toolbar/toolbar-background2.png" Stretch="Fill" />
<Viewbox x:Name="vb2" Stretch="Uniform" RelativePanel.AlignHorizontalCenterWithPanel="True" Margin="20,0,20,0">
<Button x:Name="toolbarHistory">
<Image Source="ms-appx:///Assets/MainPage/toolbar/toolbar-history.png" Stretch="UniformToFill" />
</Button>
</Viewbox>
</RelativePanel>
</Grid>
</Grid>

How to change whole Grid's background color for specific row

Something similar to this:
<Grid Background="Yellow" Width="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
</Grid>
But this would change the whole Grid. I just want to change the background color of specific row. How?
Add another Grid inside the Grid like this:
<Grid Background="Yellow" Width="300">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="0"/>
</Grid.RowDefinitions>
<Grid Background="Blue" Grid.RowSpan="2" Grid.Column="0"/>
</Grid>

Resposive Grid for Xamarin.Forms not resposive enough

I'm using a simple grid to make a menu list responsive for all resolutions. It's mainly working fine but if you see it on Android (and UWP if I resize the view to a small width) the right side has less space than the left size which has made it really ugly and unprofessional. Can you help me fix it?
This is the code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
This is how it looks like:
In Xamarin.Forms, Grid takes Padding by itself so you should set Padding Accordingly OR if you don't want Padding than set Padding=0 .
process of illumination ?:
Could you also change the background colour of your ContentPage to make sure its not applying the margin.
most likely you have something like margin="2, 0, 0, 0" somewhere in your code
<StackLayout Padding="0" Margin="0">
<Grid Padding="0" Margin="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
</StackLayout>

ListView - Data template and data source in xaml (instant changes in VS designer)

I have something like this:
<ListView >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<SymbolIcon Symbol="SomeExistingSymbol" Margin="0,0,10,0" />
<TextBlock Grid.Column="1">Some data</TextBlock>
</Grid>
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<SymbolIcon Symbol="SomeExistingSymbol2" Margin="0,0,10,0" />
<TextBlock Grid.Column="1">Some data 2</TextBlock>
</Grid>
<!--More list view items (grids)-->
</ListView>
I don't want to copy Grids over and over. I know I can use binding and data templates.
But I want to have all data defined in xaml - is that possible?
The reason is that I need to see instant changes of data in VS designer, without building project.