How to say XAML <Button Height="Auto"/> in code behind? - c#

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

Related

WPF Binding Without XAML

I have a WPF app with a "Grid Window". This window has no added XAML to it. I create a grid (columns and rows) then place a rectangle in each one all in C#.
This allows me to make a grid where I set the 'Stroke' and show locations on the grid when I set the 'Fill'.
The entire grid is set the same, in other words, if one part of the grid is red, the whole grid is red. Currently I set the grid by iterating through all of the rectangles and setting the 'Stroke' property. That works fine but seems very slow compared to most of the other operations. I would like to bind the stroke property to a variable in the C# (unless iterating is a reasonable way to handle it).
I have looked at quite a few questions here, but most want to use XAML. My code below is based off of Binding without XAML [WPF]. No errors, the grid just never shows up.
// put a rectangle in each square
for (int i = 0; i < x; i++) // for each column
{
for (int j = 0; j < y; j++) // for each row
{
// create a new rectangle with name, height, width, and starting color (transparent)
var rect = new Rectangle()
{
Name = $"rec{(i + 1).ToString("00")}{(j + 1).ToString("00")}", //column 5 row 2 -> rec0502
Height = squareSize,
Width = squareSize,
Fill = _ColorOff
};
// create the binding
var binder = new Binding
{
Source = _GridColor, // Brush that is updated on color change
Path = new PropertyPath("Stroke")
};
// apply the binding to the rectangle
rect.SetBinding(Rectangle.StrokeProperty, binder);
rect.DataContext = binder;
// place the rectangle
Grid.SetColumn(rect, i); // current column
Grid.SetRow(rect, (y - j - 1)); // same row but from the bottom (puts point 0,0 at bottom left)
// add the rectangle to the grid
grdBattleGrid.Children.Add(rect);
}
}
Even if iterating is fine, I'd still like to know what I'm doing wrong.
EDIT: The color name is chosen from a ComboBox on a separate window. This updates the user settings, which in turn throws an event my "Grid Window" is subscribed to. I convert the name to a SolidColorBrush before iterating though the rectangles.
The most simple solution would be not to have any Binding at all. Assign _GridColor to the Rectangle's Stroke. Whenever the Color property of (the assumed SolidColorBrush) _GridColor changes, it affects all Rectangles.
public SolidColorBrush _GridColor { get; } = new SolidColorBrush();
...
var rect = new Rectangle
{
Name = $"rec{(i + 1).ToString("00")}{(j + 1).ToString("00")}",
Height = squareSize,
Width = squareSize,
Fill = _ColorOff,
Stroke = _GridColor // here
};
Grid.SetColumn(rect, i);
Grid.SetRow(rect, (y - j - 1));
grdBattleGrid.Children.Add(rect);
Change the Rectangle Stroke by assigning a value to the Color property of the _GridColor Brush:
_GridColor.Color = Colors.Red;
You just need to set the DataContext one time for the Window, not for each Rectangle.
Is Binding your own view model class from INotifyPropertyChanged interface? Link to MS Docs

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.

How to measure the width of whitespace in windows store?

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.

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

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

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.

Categories