Resizing another grid row size in WPF - wpf

I am wondering is there any way to solve this by changing XAML code?
Please look at this sample picture:
What I wants to do is when user dragging GridSeparater No.1, I want to resize thr 3rd row of grid.
This is because in this application, the first and third rows are variable size, but second one is fixed size.
Is that possible?

This is possible with ResizeBehaviour="PreviousAndNext" (Link to MSDN). This enables you to specify which rows should be affected relative to the GridSplitter.
<Grid Width="300" Height="200" Background="Yellow" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<GridSplitter Grid.Row="1"
ResizeDirection="Rows"
ResizeBehavior="PreviousAndNext"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Background="Black"
ShowsPreview="True"
Height="5"
/>
</Grid>

You could try setting both MinHeight and MaxHeight to the same value on the center row which would force recalculation of the bottom section when resizing the top. Something like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="50" MaxHeight="50" MinHeight="50"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
... other content
<GridSplitter Height="3" Grid.Row="1" HorizontalAlignment="Stretch"/>
<GridSplitter Height="3" Grid.Row="3" HorizontalAlignment="Stretch"/>
</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>

Common ScrollViewer for multiple datagrid in a Screen

I have three user controls(all of them are dynamically created DataGrids) inside my main screen
i am trying to get a single scroll bar for all the three datagrids . the datagrids is created dynamically and it can be of different coloumns. So my requirement says that all datagrids should have same size. that means if datagrid2 is of bigger size, other two datagrids should grow to match with the datagrid2's size.
Here is how the basic xaml structure.
<ScrollViewer x:Name="ScrollViewer"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Grid MinWidth="400">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<userControl1/>
<userControl2 Grid.Row="1"/>
<userControl3 Grid.Row="2"/>
</Grid>
</ScrollViewer
This is the type of thing where SharedSizeGroup comes to the rescue.
Try something like this;
<ScrollViewer>
<Grid MinWidth="400">
<Grid.RowDefinitions>
<RowDefinition SharedSizeGroup="A"/>
<RowDefinition SharedSizeGroup="A"/>
<RowDefinition SharedSizeGroup="A"/>
</Grid.RowDefinitions>
<userControl1/>
<userControl2 Grid.Row="1"/>
<userControl3 Grid.Row="2"/>
</Grid>
</ScrollViewer>

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.

Resources