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;
}
Related
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.
I have am trying to set a GridViewColumn.DataTemplate to a TextBox VisualTree. The code so far is:
//GridViewColumnCollection columns
DataTemplate template = new DataTemplate();
FrameworkElementFactory elementFactory = new FrameworkElementFactory(typeof(TextBox));
elementFactory.SetBinding(TextBox.TextProperty, new Binding { Path = new PropertyPath("Position") });
elementFactory.SetValue(TextBox.MinWidthProperty, new GridLength(50));
template.VisualTree = elementFactory;
columns[1].CellTemplate = template;
When I run this code I get the following error:
50 is not a valid value for property 'MinWidth'.
on this line:
elementFactory.SetValue(TextBox.MinWidthProperty, new GridLength(50));
I also tried setting the value to just 50, but to no avail!
What am I doing wrong?
Thanks in advance.
According to MSDN, the MinWidth dependency property is of type double. You should set it to a double instead of a GridLength object.
Property Value
Type: System.Double
The minimum width of the element, in device-independent units (1/96th inch per unit). The default value is 0.0. This value can be any value equal to or greater than 0.0. However, PositiveInfinity is not valid, nor is Double.NaN.
I totally assent with bouvierr. I was working on a WPF project and this was something I also run into. Eventually, through the MSDN documentation, I noticed that the code behind concept only accepts double data type as supposed to an integer.
textBox.SetValue(HeightProperty, 120.0);
textBox.SetValue(WidthProperty, 360.0);
textBox.SetValue(FontSizeProperty, 14.0);
textBox.SetValue(MinWidthProperty, 50.0);
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;
I want to be able to set the number of lines in a multilined TextBox.
I've tried the following:
int initHeight = textBox1.Height;
textBox1.Height = initHeight * numOfLines;
But this makes it too large when numOfLines gets large. So then I tried this:
float fontHeight = textBox1.CreateGraphics().MeasureString("W", textBox1.Font).Height;
textBox1.Height = fontHeight * numOfLines;
But this was too small when numOfLines was small, and too large when numOfLines was large.
So I'm doing SOMETHING wrong... any ideas?
This would set the exact Width & Height of your multi line Textbox:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height + Convert.ToInt32(textBox1.Font.Size);
Something like this should work:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height;
This was from C# Resize textbox to fit content
What you are doing should work, but you need to set the MinimumSize and MaximumSize I am not 100% positive, but I think this constraint will still hold if height is set via code
From the documentation of Graphics.MeasureString:
To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.
As such, you should use one of these overloads, such as this one, which allow you to specify StringFormat.GenericTypograpic to get the required size.
Try this:
float fontHeight;
using (var g = textBox1.CreateGraphics())
fontHeight = g.MeasureString("W", textBox1.Font, new PointF(), StringFormat.GenericTypograpic).Height;
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.