HowTo define the "Auto" Width of the WPF GridView Column in Code? - c#

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.

Related

How set height row in excel with NPOI?

How set height row in c# with NPOI?
To specify the width of the columns I'm using XSSFSheet.SetColumnWidth, but what does the command for the height of the cells look like?
try below approach
var row = sheet.CreateRow(0);
row.Height = 10 ;
//Or
sheet.GetRow(1).Height = 10;
The height of the row is the same:
XSSFSheet.GetRow(index).Heigh {get;set;}
In addition to #kumar answer, you can set it like this
row.HeightInPoints = 16.5F;

How to get the 'Width', when set 'HorizontalAlignment = "Stretch"'?

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.

Can't set TextBox.MinWidthProperty when creating a VisualTree for GridViewColumn.DataTemplate

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);

How to set just only a side of WPF control margin?

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;

Excel Interop get width in pixels of text inside cell

I have a wide merged cell showing the title of the sheet. I also have a colored Textbox that I would like to position immediately to the right of the text in the merged cell.
The width of the title is variable so I have used a very wide merged cell.
sheet.Range["A2"].Value = title;
//Red Square
sheet.Shapes.Item("redSquare").Left += 0; // Position sheet.Range["A2"].TextWidth ???
Is there a property I can access on Range that will give me the actual text width in pixels of the text in merged cell?
As far as I know, there isn't anything in Interop that will tell you the width of text in pixels, but you can do it with the following. It seems to return a value reasonably similar to the column widths that Excel sets based on the same fonts.
Excel.Range xlRange = sheet.get_Range("A2");
Excel.Font xlFont = xlRange.Font;
string fontName = xlFont.Name;
double fontSize = xlFont.Size;
Font font = new Font(fontName, (float)fontSize);
float width = Graphics.FromImage(new Bitmap(1, 1)).MeasureString(title, font).Width;
You might need to add a few pixels here and there to make sure your textbox clears the end of the text, but I think it's about as accurate as you're going to get.

Categories