I need get a WPF control height for calculate my next control margin top, but when I try get control height with textbox1.height, this is return 'Auto' and not numbers
What can I do for get control height, when it is set 'Auto'?
You should try this:
textBox1.ActualHeight
Remarks (by #Viv)
just make sure when you query for textBox1.ActualHeight, you do it once the control is Loaded. You're going to get 0.0 if you check ActualHeight before it's setup properly
Related
I am creating a custom control in C#, and want to have a grid cell that contains a ListBox, which can be hidden or shown as desired. Hiding it is easy, I just set the Width to zero, however when I want to show it, I need to know the width that the ListBox would like to use.
I thought that DesiredSize.Width would give me this vale, but it's always zero, even after calling Measure(). Here is the code I'm using to show the ListBox...
_lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
_lb.Width = _lb.DesiredSize.Width;
Any ideas how I find the desired width?
If your ListBox is in the cell of a grid, give that grid column a name. Then in code, you can access the .ActualWidth property of that grid column and use that value to set the width of your ListBox.
That assumes of course that the width of your grid column is not set to Auto, because that would still give you a 0 value.
_lb.Width = myGridColumn.ActualWidth
You might need to subtract a little bit from the column width to make your control fit nicely.
EDIT
One thing that I've found is that the ListBox must have items added to it before it will return anything other than 0 when it is measured.
string myItem = "Don't ask for a bath in Athabaska";
_lb.Items.Add(myItem);
_lb.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double width = _lb.DesiredSize.Width;
As long as the ListBox has already been added to the window/usercontrol/grid, the above code returns a value of 227.53 for the width variable; using my defaults for font family and size.
If the ListBox has not been added to the window, or it doesn't have any items in it, it will return 0 for the .DesiredSize.Width property.
Also, if the .Visibility property is set to Collapsed instead of Hidden, the width will be 0.
Don't set the width to 0 when starting. Leave the width alone initially, set the .Visibility to Hidden. It will render to the needed width, but won't be shown. Then you can measure it and start playing around with the width.
Can I set custom ApplicationView MinWidth in my app?
Or Can I disable 500,320 and other, just set MinWidth like my current width?
Short answer, no. The user can always size a view down to the min width (and any size above it). If you don't specify the 320px min in the manifest, you'll default to 500px. If you set 320px, then the user can resize anywhere down to 320. There isn't any way to set a value in between 320 and 500.
How can I set the width of an asp:chart to be 100% the width of the page?
The chart width attribute does not like percentages.
Another solution would be to check the current resolution, how to get that from .net?
You can try using the Request.Browser.ScreenPixelsWidth property.
I am trying to create a custom user control in WPF. I want to be able to set the size manually when I later use the control within another window.
As a short test I have just made a control comprising a canvas within a grid, which totally fills the control. When initialised it draws a rectangle within itself showing its size. I then position this on a window, making it whatever size I want.
However I now have problems, as if I make the height of the rectangle I draw
this.ActualHeight
then when the control initialises this value is still 0, and so I get nothing. If instead I use
this.Height
then I get the height that I made it during design time, and not the size I have subsequently made it within the window.
The height and width seem to be set within the XAML designer, so I don't know what to do.
Is there an easy way around this?
I think what you're experiencing is how WPF performs layout and specifically how Canvas does not participate in Layout.
In your specific case you are setting the width of the Rectangle to Canvas.ActualWidth? Unless the width / height of the canvas have been explictly set then the ActualWidth/Actualheight will be zero, hence you can't get a point of reference within which to position children of Canvas. What I would do is bind the canvas width and height to its parent container (or set in code) to get the ActualWidth / ActualHeight correctly propagated.
As a point of interest try this example to understand how the WPF Layout engine works. The following code can force a height on a FrameworkElement to set the Width, Height as normal, then force a layout (measure, arrange pass) on the element in question. This causes the WPF layout engine to measure/arrange the element and propagate the Width, Height to ActualWidth, ActualHeight.
For example:
// Set the width, height you want
element.Width = 123;
element.Height = 456;
// Force measure/arrange
element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
element.Arrange(new Rect(0, 0, element.DesiredWidth, element.DesiredHeight));
// Subject to sufficient space, the actual width, height will have
// the values propagated from width/height after a measure and arrange call
double width = element.ActualWidth;
double height = element.ActualHeight;
Also see this related question for clarity. I have used the above method occasionally to measure the text in a textblock so as to position correctly on a canvas.
The problem?
<UI:PanelBrowser Margin="12,27,12,32"></UI:PanelBrowser>
WPF is ridiculous in that not manually specifying properties (Such as Width and Height) in this case causes them to have the values Doulbe.NaN. The problem is that I need to know this number. I'm not going to manually set a width and height in the XAML because that stops it from resizing.
Given the above piece of XAML (this object is a simple subclass of the Border control), how can I get the values of the Width and Height properties at run-time?
Edit :
Wow, I feel ridiculous. I read about ActualWidth and ActualHeight, but they were consistently returning 0 and 0 for me. The reason is that I was testing for these properties in the constructor of the Framework Element, before they were actually initialized. Hope this helps someone who runs into the same issue and testing fallacies. :)
Try using the FrameworkElement.ActualWidth and ActualHeight properties, instead.
The WPF FrameworkElement class provides two DependencyProperties for that purpose: FrameworkElement.ActualWidth and FrameworkElement.ActualHeight will get the rendered width and height at run-time.
You can use elements' ActualWidth and ActualHeight properties to get the values of the width and height when they were drawn.
Use VisualTreeHelper.GetDescendantBounds(Visual Reference), and it return Rect.
Then Check the height of the Rect.
Ex)
Rect bounds = VisualTreeHelper.GetDescendantBounds(element);
double height = bounds.height;
OR
Use UIElement.Measure(Size size), it will assign the Size into DesiredSize.
Ex)
myElement.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double height = myElement.DesiredSize.Height;