Fill with Measure/ArrangeOverride inside a scrollviewer - c#

I have a custom drawn control containing some plots.
I've put it inside a scrollviewer since later I want to be able to zoom the control's contents.
I want this control to have a specific minimum height and if the available visible space is bigger than that it should fill the space.
However using MeasureOverride I just get infinity for the available height (because of the scrollviewer).
How can I get the actual available visible space during MeasureOverride to stretch my control to this height?

You should not need to do any layout overriding for that, just set the alignments to Stretch and a MinHeight.

Related

Dynamically creating textbox and setting its location by pixel

In a WPF application, I'd like to create a textbox dynamically which will show in front of the application and be able to freely set its location by pixel. (The textbox is going to follow the mouse cursor).
This was easily done in Winforms on the fly but WPF makes things.. a little bit weird when it comes to setting a control's location by pixel since I have to add the control as a child of a container. I'm aware this is certainly doable on Canvas, but what I actually have is a dockpanel with a richtextbox to the left and a datagrid to the right.
So what are my options here? Do I have to use canvas? Can I get away with using dockpanel (or grid) to implement what I want here?
You can use a Canvas or a Grid. If you use a Canvas, set the Canvas.Left property and the Canvas.Top property. If you use a Grid, you'll need to set a size for your TextBox, set the HorizontalAlignment to Left, and VerticalAlignment to Top. To change the location of the TextBox, assign it values for MarginLeft and MarginTop.

wpf: resizing a control according to its content

I have a control that inherits from Grid, it is a grid of hexagons that are generated dynamically according to the properties.
each of the hexagons is a button and a child of the Grid, and they have a style that displays them as hexagons.
what I want is for the grid to change its size according to the total size of the hexagons.
(I can calculate the exact size needed, but I don't know how to set it).
Basically you've got several options. A simple one is calculating the size yourself and assigning to the Grid's Width and Height.
A more elaborate solution would be to ask yourself a question: which layout is needed for my items? There are some standard containers which do the layout themselves and can grow/shrink with the content. For example, if your objects are just aligned in a line, you can go for StackPanel.

WPF: How to find space available for Canvas?

I'm using WPF shapes to create Hexagons (for a game map) on a Canvas. After some playing around with ScrollViewer, I've decided to implement the scrolling and zoom of the map myself rather than using WPF functionality, just using WPF to get the events for mouse wheel, arrow keys etc. I'm placing the (Hex Map) Canvas as the last child inside a Dock Panel so it will get all the available remaining space. The Dock Panel will be set to be the content of the Main Window. But I want to find out how big the Canvas can be before I put any Children on the Canvas so that I can centre the screen over the Hex I want and only add the Shapes (Hexs) that can actually be seen. When zoomed out, a long way I will remove Polygons altogether and use another method of rendering and when zoomed in a long way I will add more details.
Is there any neat way of getting the available space? The only way that I can think of that will hopefully work is to get the current dimensions of the windows and subtract the dimensions of the outer elements of the Dock Panel, but that feels rather messy.
You may use the ActualWidth and ActualHeight properties of Canvas to determine size available to it. Be sure that HorizontalAlignment and VerticalAlignment are set to Stretch.

My own tableheader?

I want to make a table header:
The steps I have taken
Made a user control
Placed a TableLayoutPanel on it(this is basically a grid layout?)
Added and removed the number of columns/rows I wanted
Placed a label in each cell
For each label set its dock to fill and borderstyle to fixedsingle
However the borders are not lined up against each other but instead there is a gab, that makes the whole thing look very ugly.
The border size is also very small, how can I make it bigger?
You can remove the gaps by changing the lable margin property to 0,0,0,0 but the borders will double up in thickness where they meet (Leaving the outer border thinner)
You can disable the borders on the individual labels and use the TableLayoutPanel CellBorderStyle property to get a consistent look with several options for line styles but this will affect all cells, not just the header.
If the TabelLayoutPanel is a fixed size perhaps a background image?
I have no idea how to make the borders thicker in winforms, This is where WPF rules but beats you over the head for using it.
Mike

WinForms - How do you get the size a Control "wants" to be?

When a Control is docked in a container (like a Panel) and is too wide to be shown (so a scroll bar appears), Control.Width seems to return the visible width of the Control (the top arrow in the picture).
How do you get the width that the Control "wants" to be? i.e. its full width that you'd see if you didn't have to scroll (the bottom arrow in the picture).
alt text http://img19.imageshack.us/img19/372/size.png
There are two different properties of controls that you might find useful for this purpose. There is the DefaultSize which is the Size of the control when it is initially created, and then there is the PreferredSize which is the size the control can fit into so to speak. All controls have these properties and PreferredSize should be the one you're looking for.
Bounds is the actual size its drawn at
ClientSize is the size minus any scroll bars
Control.PreferredSize is what you want
Control.HorizontalScrollbar.Maximum, or Control.VerticalScrollbar.Maximum should return the maximum size required. It may return a slightly small amount as it may cut padding off.

Categories