I had booked a Grid with HorizontalAlignment = "Stretch"
And now, I want to know what is its Width ?
I want to do this by C # at runtime.
Thanks in advance.
Let's say that you have the following grid:
<Grid Name="gvDummyContent" HorizontalAlignment="Stretch" />
An you want to get the height and width of it. You can do it like this:
double actualWidth = gvDummyContent.ActualWidth;
double actualHeight = gvDummyContent.ActualHeight;
Be sure that you are calling these properties after the controls are loaded.
Related
How can i measure the width of whitespace in winrt.
I'm trying the below code for calculate the size,
For example,
var text = " ";
TextBlock txtBlock = new TextBlock();
txtBlock.Text = text;
txtBlock.FontSize = 14;
...
txtBlock.Measure(size);
var actualWidth = txtBlock.ActualWidth;
But i'm getting width as 0. Please any one help me.
Call Measure() then Arrange() and then ActualWidth and ActualHeight will be updated. This works for me.
Is there any way adjust padding in a WPF control based on the WrapPanel width, something like Windows Explorer does when the window is resized.
Here are a couple of this examples:
If you are using an ItemsControl you can define an ItemsContainerStyle and set it's MaxHeight and MaxWidth values .
By doing so the items can grow to a limited size and a padding effect would occur.
I would recommend setting the Width property of the ItemContainerStyle using a Binding and Converter to determine the correct size for each item.
In your example, it looks like your item sizes are static and you only want to distribute the remaining space, so bind the Width property to WrapPanel.Width / NumberOfItems (which is the total number of ItemWidth that can fit into WrapPanel.Width)
Here's a quick example that uses 100 as the item width :
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Width"
Value="{Binding ElementName=MyWrapPanel,
Path=ActualWidth,
Converter={StaticResource MyCustomWidthConverter},
ConverterParameter=100" />
</Style>
</ItemsControl.ItemContainerStyle>
And the converter would look something like this (I may have syntax errors here since I'm not using an IDE to verify the code) :
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// TODO: add error handling
double totalWidth = (double)value;
double itemWidth = (double)parameter;
double numItems = Math.Floor(totalWidth / itemWidth);
double availableExtraSpace = totalWidth - (numItems * itemWidth);
double paddedItemWidth = itemWidth + (availableExtraSpace / numItems);
return paddedItemWidth;
}
I'm trying set a margin of a Image Control top margin, I can get this value with Margin.Top, but why can I set this with image1.Margin.Top = 5;?
How to can I set just this only value?
This is because the property accessor does not give you a reference to the object. It is simply a wrapper around a DependencyProperty, which returns the value via GetValue. If you want to change that item, you must do this:
Thickness margin = image1.Margin;
margin.Top = 5;
image1.Margin = margin;
How can you set Height="*" and Height="Auto" in code behind?
For setting Height = "Auto" on most controls, you want to assign the value with double.NaN.
Example:
element.Height = double.NaN;
Setting Width/Height = "*" ( is a slightly different matter, since it only applies to a select few elements (ColumnDefinition and RowDefinition for example). The type of the Width/Height value is GridLength, rather than double.
Example (more are given on this MSDN page:
column1.Width = new GridLength(1, GridUnitType.Auto); // Auto
column2.Width = new GridLength(1, GridUnitType.Star); // *
I want to define the "Auto" width of a GridView Column in the code. How can I do that?
var grid = (GridView)myListview.View;
grid.Columns.Add(new GridViewColumn
{
Header = "My Header",
DisplayMemberBinding = new Binding("MyBinding"),
Width = ??? // Auto
});
GridViewColumn's Width property is of type double, but according to the MSDN page you can set it to Double.NaN ("not a number") to tell it to auto-size.
If you do that, you have to ask for its ActualWidth if you want to know the width it has auto-sized to.
In case you're looking to do the same thing in code for the Width property of a Column of a normal Grid control, use GridLength.Auto.