How could I set the value of the Height property of a WPF control in C# code to "Auto"?
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
I want to reproduce this behavior in the code behind. Any ideas?
Perhaps this link will help you.
At times, you may want to
programmatically set the Height or
Width of a WPF element to Auto in
code. To do this, just use the
Double.NaN (Not a Number) value.
For example, in C#:
this.txtName.Width = Double.NaN;
You can use
RowDefinition rd = new RowDefinition();
rd.Height = GridLength.Auto;
ContentGrid.RowDefinitions.Add(rd);
While the question has been answered with the code-behind (as asked), here is the same solution in XAML for basic control properties who don't have "auto":
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Height="{x:Static sys:Double.NaN}"
Posting because google took me here when searching for XAML solution for a TextBox (auto doesn't exist).
Related
This is my XAML Grid:
<Grid VerticalOptions="Center" Padding="15,0,15,0" x:Name="grid_forPic" >
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/> <!--neds to be overridden-->
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
Now I need to completely override the row definitions at position 5.
I am adding a picture to said row and even when I set the height of this row in XAML the picture will completely ignore the set height from XAML and just take its own hight. Now I believe forcing the row definitions again after the pic has been set should be at least a workaround.
Can someone overwrite this for me in C#? thank you!
If you want to change the Height of the Grid.RowDefinitions,you could try below method:
grid_forPic.RowDefinitions[4].Height = new GridLength(100);// here change the height of the fifth row to 100
grid_forPic.ForceLayout();
I am using a GridView to display some products.
When I set the DataTemplate I use Width and Height with static values and define some rows for the data I want to display.
<Grid Width="97" Height="180"">
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
The first row is for a 80x80 image and the second for the price.So far, so good.
Then I face the problem that even if the data shown (with the name of the product) in the last row, with a TextBlock, is just one line, the GridViewItem takes the value defined from the height property.
Is there a way for the height to adjust on TextBlock's Text. I tried setting Height to Auto, not including at all for the Grid and the TextBlock but then the text is not appearing whole on the screen.
I have a feeling what you want is the text to wrap around. You can achieve this by setting the text to wrap, like in the code demonstrated below:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
<RowDefinition Height="20"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="Red" />
<Grid Grid.Row="1" Background="Blue" />
<TextBlock Grid.Row="2" TextWrapping="Wrap" Text="This is some text which will not fall off the edge because it is set to wrap around, and will continue filling down until the end of the screen because the grid is set to auto" />
</Grid>
In WPF, how can one set the height of menu to fit , instead of like this below where the gray shade goes down
<Window x:Class="XAML_Concepts.GridLayout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="GridLayout" Height="600" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Menu IsMainMenu="True" Grid.Row="0" Grid.ColumnSpan="2" Grid.RowSpan="1" >
<MenuItem Header="File">
<MenuItem Header="_New"></MenuItem>
<MenuItem Header="_Open"></MenuItem>
</MenuItem>
<MenuItem Header="Edit"></MenuItem>
</Menu>
</Grid>
</Window>
I just need it to look a normal menu, like one in windows explorer, without any gray shading below. I have tried manipulating row size, menu size, and several other attributes but I' missing something simple.
When I set the Grid''s row height to , say 20px , this is what I get which I am not pleased with because A-I don't want to enter an absolute height and B-The shade doesn't look right, it is like the background gradient is smearing..
Thanks
Try using this:
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
This will cause the first row to shrink to only as much space as is necessary to display the content.
You must set the height of the row that your menu is located in to Auto. Like this:
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
When a row or columns width or height is set to Auto, it only takes as much space as it needs.
I have defined grid in xaml like this:
<Grid Name="grdMoney" HorizontalAlignment="Center" Margin="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
... Content, many Textboxes in each row
</Grid>
Now I just want to highlight some row by changing background in that row. But I can't find out how can I get exact row from grid in code. I think it's easy but I am googling for last 15minutes and can't find it. Maybe something with grdMoney.Childer[number_of_row]? Thanks for help
Grid is a Panel. Panels are responsible for layouting, nothing more.
If you want to update background - put content of every row to Border, and find appropriate Border like that (i wrote code here, and didn't test it, but should work):
int desiredRowId = 2;
foreach(var child in grdMoney.Children.OfType<Border>())
{
if (Grid.GetRow(child) == desiredRowId)
{
child.Background = new SolidColorBrush(Colors.Red);
}
}
I've probably'll do this in xaml using DataTriger
In data triger you can check your condition and apply appropriate background
I have a simple xaml control with the following Grid Row definition:
<Grid.RowDefinitions>
<RowDefinition Height="15*" />
<RowDefinition Height="60*" />
<RowDefinition Height="20*" />
<RowDefinition Height="20*" />
<RowDefinition Height="15*" />
</Grid.RowDefinitions>
Rows 1-3 each hold a text block which may or may not have text in it. In the code behind I want to minimise the RowDefinition if there is no text. Essentially I have the following in my code behind:
if(textblock.Text != ""){
grid.RowDefinitions[elementRow].Height = new GridLength(20, GridUnitType.Star);
}
else{
grid.RowDefinitions[elementRow].Height = new GridLength(0, GridUnitType.Star);
}
I want rows 0 and 4 to stay as they are defined in the xaml. Unfortunatly this does not work even though there is text in the text block on row 2 nothing is displayed.
Am I doing something wrong.
Any help is appreciated,
James
Don't use the star notation, use Auto for your RowDefinitions. If the TextBlock.Text is empty, set the Visibility of the TextBlock to Visibility.Collapsed. The grid row will then automatically shrink to nothing.
This is not the answer to your question, just some info.
The * in the Height (or width for columns) means that the row (or column) width Height="*" (or Width="*") will take up the rest of the space. So if you have a grid with 4 rows in a grid with Height="100", if you do this:
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="10" />
<RowDefinition Height="10" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
The row width Height="*" will be 70 DIUs (device independent units).
Adding a number before the asterisk (Height="2*") only works if there are more than one rows using the asterisk, the number before the asterisk indicates how much more space will that specific row take (2* = twice as much, 3* three times as much, so on...). I. E.:
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="10" />
<RowDefinition Height="2*" /> <!-- this row will be twice as tall as the one below -->
<RowDefinition Height="*" />
</Grid.RowDefinitions>
Here the 3rd row will have a height of 54 DIUs (twice as much as the 4th row which has a height of 26 DIUs approx.), both heights sum 80, which is the rest of the space of the grid (10 + 10 + 26 + 54 = 100, the grid height).
BTW, I agree with Charlie's answer.
You can put your items inside a UniformGrid with Columns="1" And make the TextBox Visibility to collapsed when you get emptry text.
<UniformGrid Columns="1">
<TextBlock Text="AAAA" Visibility="Collapsed" Grid.Row="0"/>
<TextBlock Text="BBBBB" Grid.Row="1"/>
<TextBlock Text="CCCCC" Grid.Row="2"/>
<TextBlock Text="DDDDD" Grid.Row="3"/>
<TextBlock Text="EEEE" Grid.Row="4"/>
</UniformGrid>