Expand a control to fill grid column - silverlight

I'm having a problem getting a panelbar to expand it's width to match the Grid column it occupies. The offending code is the second appearance of a panelbar with an item header named "Operators". I can set the width explicitly to fill the column, but that would only show correctly for my specific resolution and is not the solution I'm looking for. I would just like it to stretch on it's own. Code snippet below.
<telerik:RadTabControl telerik:StyleManager.Theme="Windows7" Grid.Column="2">
<telerik:RadTabItem Header="Add/Edit">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock FontSize="20" Grid.Row="0">Organization Name Here:</TextBlock>
<my1:QueryBuilder Height="Auto" Width="Auto" Grid.Row="1"></my1:QueryBuilder>
<telerik:RadPanelBar ExpandMode="Single" Grid.Column="1" Grid.Row="1" Width="Auto"
VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<telerik:RadPanelBarItem Header="Operators"></telerik:RadPanelBarItem>
</telerik:RadPanelBar>
</Grid>
</telerik:RadTabItem>
</telerik:RadTabControl>

You Second ColumnDefinition should have Width of "*" not "Auto" as you have currently.

Related

Fixed header and footer outside scrollview

I have a grid that has a header, content and a footer:
<Grid.RowDefinitions>
<RowDefinition Height="55"/> <!--HEADER-->
<RowDefinition Height="*"/> <!--CONTENT-->
<RowDefinition Height="55"/> <!--FOOTER-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.ColumnSpan="3"></Rectangle>
<ScrollViewer Grid.Row="1" Grid.Column="1" >
<Grid>
...
</Grid>
</ScrollViewer>
<Rectangle Grid.Row="3" Grid.ColumnSpan="3"></Rectangle>
</Grid>
I want to have it so that the header and footer are fixed and the content is scrollable. Within the second nested grid there is a lot of content, hence the scroll view. When I run the application, the scrollviewer still scrolls with the header and footer! I can't figure out what I'm doing wrong, is there a better layout I should be using?
Please let me know! I'd rather not use C#.
Thanks!
Unless you are doing something strange either around the XAML you have posted or inside the nested Grid object, your XAML works as you intended.
I very slightly modified your XAML, just to visibly show your header and footer, and to add some content to your inner grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55"/><!--HEADER-->
<RowDefinition Height="*"/><!--CONTENT-->
<RowDefinition Height="55"/><!--FOOTER-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.ColumnSpan="3" Height="55" Fill="Red"/>
<ScrollViewer Grid.Row="1" Grid.Column="1" >
<Grid >
<ListBox ItemsSource="{Binding Path=myItems, FallbackValue='123456789abcdefghijklmno'}"/>
</Grid>
</ScrollViewer>
<Rectangle Grid.Row="3" Grid.ColumnSpan="3" Height="55" Fill="Blue"/>
</Grid>
Shown below is the result. Your ScrollViewer scrolls just fine while leaving the header and footer in place.
I'm not sure what else you have going on in your window, but this is the only XAML in the window I used for testing, and it works perfectly. As a note, I limited the height of the window to '400' so the inner grid did not continue to grow since it's height was set to *. You can achieve the same result by setting a maximum height on your outer Grid.

WPF: auto resize ListView width according Window size

So i have this ListView inside TabControl:
<TabControl>
<TabItem Width="70" Height="70" Margin="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<ListView>
...
</ListView>
</Grid>
<Grid>
</Grid>
</Grid>
</TabItem>
</TabControl>
My window ResizeMode is CanResizeWithGrip and when i resize my application i want my ListView to auto resize to according my Window width.
I try to define my ListView HorizontalAlignment Stretch bu when my application width changed my ListView with not.
I believe since your ListView is nested within other controls, those controls would need their HorizontalAlignment set to Stretch. I'm on my phone right now so I'll try out your XAML on my system when I get home and update my answer if I find something else is the culprit.
Edit: I've copied your code into VS and its stretching properly for me. I don't have any contents to check but if I set
<TabControl>
<TabItem Width="70" Height="70" Margin="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<ListView Background="Black">
</ListView>
</Grid>
</Grid>
</Grid>
</Grid>
</TabItem>
</TabControl>
I can see that the control is in fact stretching when I resize the window. Now I'm unsure of what the issue you are running into is. Any chance you could post more details?

WPF datagrid height is infinite

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.

Textbox alignment inside Groupbox (WPF)

I'm having trouble with placing a textbox inside a groupbox (which is inside a grid). Since groupbox can contain only one children I created a grid to place labels and textboxes. The problem is - the textboxes created inside the groupboxes are left aligned according to the 2nd column of the grid inside the groupbox. I need the textboxes to be aligned left in the 2nd column of the main grid.
My xaml code
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<GroupBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
<GroupBox.Header>
<Label Content="Header"></Label>
</GroupBox.Header>
<Grid Grid.Row="0" Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="Label1:"/>
<TextBox Grid.Column="1" />
<Label Grid.Column="0" Grid.Row="1" Content="Label2:"/>
<TextBox Grid.Row="1" Grid.Column="1"/>
</Grid>
</GroupBox>
</Grid>
Output for this code
You could see the textboxes in the column 1 of the main grid. If you look at the image, there is a radio button below that row which is in the second column. I need the textboxes to be aligned with that radiobutton I need them in the second column.
Width="Auto" means the Column will automatically take a width.
You need to use something like this instead;
`<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>`
Have a look at this ColumnWidths for further explanation if needed.
To move the TextBoxes to the next Column in the main Grid, you need to remove them from the GroupBox and add them to the main Grid instead. You haven't posted the whole XAML so it's difficult to advise further.
if there are going to be only two columns in your grid then just below column definitions for your inner Grid:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50*"/>
<ColumnDefinition Width="50*" />
</Grid.ColumnDefinitions>
As you want to align with the radio buttons that must be in second column of main grid then for ColSpan=2 this will perfectly.
When Width is set to Auto for a ColumnDefinition of a Grid it
will take the width of element defined in the Column , So that's
the first thing you should remove a you are not using fixed Width
for TextBoxes.

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