WPF datagrid height is infinite - wpf

I have two datagrids (data grid 1 and 2) which are being bound from a separate User Control:
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="0" >
<local:DATAGRID1 x:Name="DATAGRID1" /></Grid>
<Grid Grid.Row="1" Grid.Column="0">
<local:DATAGRID2 x:Name="DATAGRID2" /> </Grid>
<Grid Grid.Row="0" x:Name="AddURLContainer" Grid.Column="1" >
<StackPanel>
<local:test1 x:Name="NewQueryControl"/>
<local:test2 x:Name="AddURLControl" />
</StackPanel>
</Grid>
</Grid>
But for some reason the data grids stretch longer than the window and don't constrain within the windows height. I've attempted to put the Datagrids in a scroll viewer but the scroll bar also goes out of the window and doesn't constrain. I can't figure out why its doing this.
The opening tags of the actual data grids are (and they are wrapped in a User control not a Stackpannel):
<DataGrid AutoGenerateColumns="False"
IsReadOnly="True"
SelectionMode="Single"
>

Seemed to be fixed if I change the above main grids row properties from:
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
To:
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
Strange fix but it works.

Why are using * width and heigt?
Use Auto:
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
Auto set the size to it's allocated content.

Related

How to get half of the full window using grid.row in xaml?

I want to get a structure like this:
So i tried this:
<!-- Canvas -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<!--Toolbox & poperties -->
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<Button Content="sdf"/>
</StackPanel>
<StackPanel Grid.Row="1">
<Button Content="sdf"/>
</StackPanel>
</Grid>
</Grid>
But I couldn't get the half of the height using grid. What else I could do?
You can try the following markup and use Grid.RowSpan to extend StackPanel into two rows
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="4*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Content="sdf"/>
<Button Grid.Column="0" Grid.Row="1" Content="sdf"/>
<StackPanel Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"/>
</Grid>

Wpf - Grid using row and column definition - How to ignore columns for some rows

I'm trying to use the grid row / column definitions in my wpf application. At the moment, I need to implement a list view inside a GroupBox. Here I need to ignore the column definitions i set in the top in the view.
Row and column definitions:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="260" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="180" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="180" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Here you see I have a rowDefinition with a height of 260. This should contain my list view. The problem is that it's inside the columns I have made and therefore it won't take all space. Is there somehow a setting so that this row will ignore the columns I have set? I still want the columns to be used for the other rows.
Here you see a picture of how it looks:
Hope someone can help, good day.
Just use attached property Grid.ColumnSpan :
<ListView Grid.ColumnSpan="6"/>
It will extend your ListView for 6 columns.
Simple advice about your UI:
I suggest you to create you resizable XAML, not static. I mean it is not good:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="260" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="180" />
<ColumnDefinition Width="20" />
<ColumnDefinition Width="180" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
However, it is better:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="3*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="0.5*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
It gives resizable UI(XAML) at any display.
You can set Grid.ColumnSpan="6" on your listview. It will expand in the row.
<ListView Grid.ColumnSpan="6"/>
depends on how you want to display the listview.
you can add another grid, just to be safe, that you will want to add something later on that row.
<Grid grid.Row="6" Grid.Column="0" Grid.ColumnSpan="6">
<ListView> </ListView>
</Grid>

How to build a 3x3 grid with squared center cell in XAML

I am trying to build a 3x3 grid with XAML (for a Windows Phone application) where the center cell should be a square. I have tried the following but it does not work:
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="5*" x:Name="centerColumnDefinition" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="{Binding ElementName=centerColumnDefinition, Path=ActualWidth}" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Canvas Grid.Row="1" Grid.Column="1">
...
</Canvas>
</Grid>
Any suggestions for a working solution?
Greetings from Germany,
Tobias
Try this
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition x:Name="centerColumnDefinition"
Width="5*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Rectangle x:Name="CenterRect"
Grid.Row="1"
Grid.Column="1"
Height="{Binding ElementName=CenterRect,
Path=ActualWidth}"
HorizontalAlignment="Stretch" />
</Grid>
It can be tricky to reference column and row definitions because they are not "real" objects in the visual tree. This method avoids the problem by using a separate object in the center cell that can be used to get the proper sizes.
You should be able to replace the rectangle with another type of control if you want, or just leave it and embed your content inside the rectangle.
ActualHeight and ActualWidth are not set until the control is measured and arranged. Usually there is nothing in InitializeComponent() that causes a measure, so you will need to set the Height of your rows after it's calculated. You can do the re-sizing on Loaded event.
Source: https://stackoverflow.com/a/1695518/546896

how to define column definitions in a grid?

i have a grid with fixed number of rows but the columns are not fixed. So how do i set the column definitions.Is there any way to write it in xaml?
You have both rows definition and columns definition:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="2*" />
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid

Grid inside Grid in XAML

I want to have a childGrid in second column of parentGrid (in childGrid I want to have two columns: first for label, second for textbox)
How can I do Something like that? I tried the following code:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Height="*"/>
<ColumnDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column=1>
<Grid.ColumnDefinitions>
<ColumnDefinition Height="*"/>
<ColumnDefinition Height="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</Grid>
Based on your code, just fixed up a little:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
</Grid>
</Grid>
Note that ColumnDefinition don't have a Height - they have a Width. You also need to define the ColumnDefinitions and RowDefinitions separately - you have them mixed together in your outer grid. I removed the RowDefinitions from the outer grid because you don't appear to be using them. Your inner grid has two columns and four rows.
You might find this useful. Try pasting this into a page using Kaxaml and playing around with the various parameters of the objects in the outer Grid. I find using Kaxaml for prototyping and experimenting with XAML layouts indispensable.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!--
When I'm composing grids in XAML, I group things together by type, not by where
they live in the grid. This turns out to make a lot of maintenance tasks
easier.
Also, since Grid.Row and Grid.Column default to 0, a lot of people (and tools)
omit them if that's their value. Not me. It lets me quickly check to make
sure that content is where I think it is, just by looking at how it's organized
in the XAML.
-->
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Background="Lavender" Padding="10" HorizontalAlignment="Stretch">Here's the first row of the outer grid.</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Background="Lavender" Padding="10" HorizontalAlignment="Stretch">Here's the third row of the outer grid.</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Background="AliceBlue" Padding="10">Here's the first column of the second row.</TextBlock>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.ColumnDefinitions>
<!--
This part's pretty important. Setting up the SharedSizeGroups for these
two columns keeps the labels and text boxes neatly arranged irrespective of
their length.
-->
<ColumnDefinition SharedSizeGroup="Label"/>
<ColumnDefinition SharedSizeGroup="TextBox"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0">First label</Label>
<Label Grid.Row="1" Grid.Column="0">Second label</Label>
<Label Grid.Row="2" Grid.Column="0">Third label, containing unusually long content</Label>
<TextBox Grid.Row="0" Grid.Column="1">First text box, containing unusually long content</TextBox>
<TextBox Grid.Row="1" Grid.Column="1">Second text box</TextBox>
<TextBox Grid.Row="2" Grid.Column="1">Third text box</TextBox>
</Grid>
</Grid>
It might come a little confusing how to put controls in sub grids. Here is an example.
We have 3 * 3 cell grid. And then center cell is further divided in 3 rows where each row has a button.
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Button1"/>
<Button Grid.Row="1" Content="Button2"/>
<Button Grid.Row="2" Content="Button3"/>
</Grid>
</Grid>
Result:
Phenevo, I've done XAML UI design extensively this year. Try this out, you can easily migrate the code to either a Window or a UserControl. I color-coded the grids and panels so that you could affirm their layout in real time -- blow away the background parameters when you're satisfied.
<UserControl
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="UatControlLibrary.sampleChilGrid"
x:Name="UserControl"
MinWidth="400"
MinHeight="300"
Width="auto"
Height="auto">
<Grid
x:Name="LayoutRoot">
<Grid
x:Name="parentGrid"
Width="auto"
Height="auto"
Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="1*" />
<ColumnDefinition
Width="1*" />
</Grid.ColumnDefinitions>
<Grid
x:Name="chilGrid"
Width="auto"
Height="auto"
Background="Black"
Grid.Column="1"
Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="1*" />
<ColumnDefinition
Width="1*" />
</Grid.ColumnDefinitions>
<StackPanel
x:Name="stkpnlLabels"
Background="White"
Grid.Column="0"
Grid.Row="0" />
<StackPanel
x:Name="stkpnlTextboxes"
Background="Blue"
Grid.Column="1"
Grid.Row="0" />
</Grid>
</Grid>
</Grid>
</UserControl>

Resources