Column widths are specified in different ways (Stars, Auto etc)
How to get the width in pixels of a specific column?
GridLength l=tip.basis.ColumnDefinitions[0].Width;
You can use the ActualWidth or ActualHeight property of elements to get the width/height of elements. This answer describes the difference between `'ActualWidth' and 'Width'.
So in the above example it would be:
Double width = tip.basis.ColumnDefinitions[0].ActualWidth;
And also keep in mind that WPF uses Device Independent Pixels, as described in this answer.
The ActualWidth property should provide the width of the column in Pixels
MSDN Page
Use the ActualWidth property to get the width. It represents
The width of the column in device-independent units (1/96th inch per
unit).
Related
I am using the Microsoft Chart Control.
I am trying to adjust the height of the chart area programmatically. Depending on the rows. Because the Microsoft Chart control requires a height, which cannot be in percentages, nor Auto. Depending on the selected query, the chart can have 1 bar or it can have 100's. This is why I need to adjust the height because bigger data sets appear squished and not readable.
What I have tried is adding a counter heightCounter which is the number of bars on the chart.
int newHeight = (heightCounter * 10);
// Maximum height aloud on Chart Control
if (newHeight > 32767)
newHeight = 32767;
Chart1.Height = newHeight;
This sometimes works O.K. with smaller data sets
but when I have very large datasets the title looks very spaced out because the height is too large.
Is there a proper way to achieve this?
I think that the way you are approaching the problem is along the right lines, taking a count of the number of series' in the datasource in order to obtain a proportional height.
However, I would be inclined to declare the width of each bar as described here. This would give you more control than using a static multiplier such as the 10 you have.
I would also be inclined to set the height permanently as the calculated height, unless of course the minimum height is a desired feature.
Also, as a side note, isnt increasing by 10 for each quite significant? Have you tried halving that? If I remember rightly, the width of the bars will autmatically scale themselves (if not specifically declared by you).
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.
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.
When I read the width value of my GridColumn I get the default value set in xaml (640 pixels). But the column is much wider than that in runtime because it stretches according to the width of the browser. In runtime I want to scale some of my controls to match that but when I read the value of my column it still gives me the default value 640. Until I actually resize the column with a gridsplitter the correct value get through. Anyone know how I can get the REAL width of the column during runtime without me having to resize it first?
Double a = this.Column1.Width.Value; // Gives me the default value of 640 while the column is much wider than that.
Try
Double a = this.Column1.ActualWidth.Value;
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;