How to style a page in XAML - wpf

I'm really new to XAML and Metro and my old HTML skills are not in my advance.
What i want to achive is to have 3 "rows", the top row as some kind of header, the last row as some kind of footer and then a scrollable content area in the middle.
How can I achive this in XAML for Metro?
I've tried the StackPanel but I can't get the middle one to stop expanding and putting my "footer" out or the screen.

read more about WPF Layouts,
Grid in WPF is similar to Table in HTML, you should do this if you want header and footer.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/> <!--Header-->
<RowDefinition/>
<RowDefinition Height="50"/> <!--Footer-->
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Header"></TextBlock>
<ScrollViewer Grid.Row="1">
<!--your Controls-->
</ScrollViewer>
<TextBlock Grid.Row="2" Text="Footer"></TextBlock>
</Grid>

Try this,
<Grid x:Name="GridName">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*" />
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Header"/>
<StackPanel Orientation="Vertical" Grid.Row="1">
<!-- Other Controls -->
</StackPanel>
<TextBlock Grid.Row="2" Text="Footer" />
</Grid>

Related

WPF DataGrid in Tab Control Expanding Beyond Window

I have a WPF Window which has a Tab Control, in the third row of Grid, which contains a DataGrid. The DataGrid is being populated with data from EF and when the data is loaded, the DataGrid is going beyond the bounds of the window.
I've tried various solution (e.g. setting the Vertical/Horizontal Alignments to Stretch) but nothing I have tried worked. Below is a snippet of code up to the first open tag of the DataGrid (there are actually three tabs each with a DataGrid, but they are all the same, just bound to a different data source). With the below XAML, the width binds correct (i.e. the DataGrid doesn't go beyond the right side of the window) but the horizontal part of the DataGrid does go beyond the bottom of the window:
<Grid Margin="5,0,5,0" Background="Blue" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<user_controls:Spinner x:Name="spinner" Grid.RowSpan="3" Panel.ZIndex="1000"/>
<Menu Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top">
<MenuItem Header="Exit" Click="Exit_Click"/>
<MenuItem Header="Save" Command="{StaticResource SaveCommand}"/>
</Menu>
<GroupBox Grid.Row="1" Header="Server Type" Margin="610,0,0,0">
<StackPanel Orientation="Horizontal" Margin="0,5,0,0" HorizontalAlignment="Right">
<RadioButton x:Name="rbTestServer" GroupName="ServerType" Content="TEST" Foreground="Red" IsChecked="true" Checked="ServerType_Checked"/>
<RadioButton x:Name="rbProductionServer" GroupName="ServerType" Content="PRODUCTION" Foreground="Green" Margin="10,0,10,0" Checked="ServerType_Checked"/>
</StackPanel>
</GroupBox>
<TabControl x:Name="tcTables" BorderBrush="Red" BorderThickness="5" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="371" Height="auto" MinWidth="782" Width="auto" SelectionChanged="TcTables_SelectionChanged">
<TabItem x:Name="tiChargeType" Header="Charge Type">
<DataGrid x:Name="dgChargeType" Background="#FFE5E5E5" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Source={StaticResource vsChargeType}}" Margin="2,10,10,10" RowDetailsVisibilityMode="VisibleWhenSelected" CellEditEnding="CellEditEnding">
Any help/suggestions would be greatly appreciated.
Thank You
You have three rows in your grid:
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
Because the third is set to Auto height, it tells it's content to go as big as it likes.
This is the cause of your problem.
Change that to:
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
Then your tab control and hence the datagrid in it will have a height limited to whatever is left after rows 0 and 1.

Not able to set resizing limit to grid splitter

I have two grids horizontally and i want to resize the first grid horizontally. so i used Grid Splitter like below,
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition MinHeight="5"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding ItemsCollection}" AutoGenerateColumns="True"/>
<GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" Background="Red" />
<DataGrid Grid.Row="2" ItemsSource="{Binding ItemsCollection}" AutoGenerateColumns="True"/>
</Grid>
now i'm able to resize it . but i could resize it fully(to the header of a grid). i want to set limit to resizing. i dont want to hide entire grid by resizing. i want to show header and first row always.
I have checked your scenario and Height="Auto" won't work. I have updated grid rows definition. please check below.
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="100"/>
<RowDefinition Height="5"/>
<RowDefinition Height="*" MinHeight="100"/>
</Grid.RowDefinitions>
<DataGrid ItemsSource="{Binding ItemsCollection}" AutoGenerateColumns="True"/>
<GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" Background="Red" />
<DataGrid Grid.Row="2" ItemsSource="{Binding ItemsCollection}" AutoGenerateColumns="True" />
</Grid>

Grid RowDefinintion fixed Height ignored

I have a simple Grid with two rows, the first having a fixed height. Inside, I have an element with RowSpan="2", and on top another element which should reside only in the first row:
<Grid Background="Lime">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.RowSpan="2" Height="50" Fill="Blue"/>
<TextBlock Grid.Row="0" Text="Foo" VerticalAlignment="Center" Background="Red"/>
</Grid>
However, the actualheigth of the first row simply ignores the Height setting, beeing much larger than expected.
Is this a bug in the Grid? How can I workaround this?
I think you want something like this:
<Grid Background="Lime">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.RowSpan="2" Height="50" VerticalAlignment="Center" Fill="Blue"/>
<TextBlock Grid.Row="0" Text="Foo" VerticalAlignment="Center" Background="Red"/>
</Grid>
I'm not sure if having a RowSpan that covers a RowDefinition with Height="*" would work out.
i also tried this a few times, but it won't work. I think it's by design. Can't you just put the rectangle as a parent of the grid in the xaml?
It does not seem to work this way
Instead, I simply used the VerticalAlignment property to move the TextBlock to the top, and completely removed the RowDefinitions:
<Grid Background="Lime">
<Rectangle Height="50" Fill="Blue"/>
<TextBlock Grid.Row="0" Text="Foo" VerticalAlignment="Top" Background="Red" Height="20"/>
</Grid>
In this case you must use VerticalAlignment to stretch in order to fill your RowDefinition's Height.
<Grid Background="Lime">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Rectangle Grid.RowSpan="2" Height="50" Fill="Blue"/>
<TextBlock Name="texto" Grid.Row="0" Text="Foo" VerticalAlignment="Stretch" Background="Red"/>
</Grid>
On this way you will see your TextBox stretched to the Row height.

WPF Layout - Fill Height, Auto Scrollbar

The Grid at the bottom contains a ListBox. It stretches vertically, but the scrollbar does not appear when it reaches the bottom.
Layout --
<RibbonWindow ResizeMode="CanResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel>
<Ribbon ... />
<ListBox
VerticalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</StackPanel>
</Grid>
</RibbonWindow>
I have heard that StackPanels can cause this behavior, but replacing it with a Grid causes its own set of issues.
EDIT --
This Layout works -
<RibbonWindow ResizeMode="CanResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Ribbon Grid.Row="0" />
<ListBox Grid.Row="1"
VerticalAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="Auto"
/>
</Grid>
</RibbonWindow>
Turns out I needed the Grid.Row="x" tags, and then I could remove the StackPanel, and everything worked.

WPF: Inner content wont scroll

I have a Window with a Grid inside:
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" MinHeight="70" />
<RowDefinition Height="*" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<ContentControl Grid.Row="0" Content="{Binding ChildViewModel.View}" />
<DockPanel Grid.Row="1" Visibility="{Binding SearchResultViewVisibility}">
<GridSplitter DockPanel.Dock="Top" Background="LightGray" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Top" IsTabStop="False"/>
<Views:SearchResultView DataContext="{Binding SearchResultViewModel}" />
</DockPanel>
<UserControls:GradientBackgroundControl Grid.Row="2" Height="25">
<Validators:FocusSummaryControl x:Name="FocusSummary" ValidateOnlyFocusedElement="False" />
</UserControls:GradientBackgroundControl>
</Grid>
The ContentControl gets a UserControl with this Grid set:
<Grid KeyboardNavigation.TabNavigation="Local">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="80" />
<ColumnDefinition Width="80" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Row="0" Grid.ColumnSpan="4">
<StackPanel>
...
</StackPanel>
</ScrollViewer>
The problem now is, that the ScrollViewer in the UserControl doesn't scroll. The content of the UserControl set to the ContentControl is heigher and the overflow ist just hidden.
If I am not wrong, StackPanel requires a Height to be set for scroll functionality to work because StackPanel, by design, grows in one direction (based on Orientation).
To confirm whether this is the cause of your problem, please test by setting the height of StackPanel to a fixed height. Alternately, you may want to replace the StackPanel with say DockPanel and see the behaviour. Also there is a ScrollViewer.CanContentScroll property that you may want to fiddle with.
Let us know the result of this test.
I think you need to rearrange things a little bit. My suggestions (I'm sure there are infinite variations that would work):
First, add a new row to your grid (Height="Auto") and set the height of your top row (with your ContentControl in it) to "*"
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="70" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
Second, move your GridSplitter out of the DockPanel. Put the splitter in row 1 and the dockpanel in row 2.
<ContentControl Grid.Row="0" Content="{Binding ChildViewModel.View}" />
<GridSplitter Grid.Row="1" Background="LightGray" Height="5" HorizontalAlignment="Stretch" VerticalAlignment="Top" IsTabStop="False" ResizeBehavior="PreviousAndNext"/>
<DockPanel Grid.Row="2" Visibility="{Binding SearchResultViewVisibility}">
<Views:SearchResultView DataContext="{Binding SearchResultViewModel}" />
</DockPanel>
Note that you'll probably also have to set the ResizeBehavior for your GridSplitter as shown above. I hope this will get you close to what you want.

Resources