How to set the PREFERRED size of a WPF control - wpf

Is there a way to set the preferred size of a WPF control without setting its maximum size?
I normally set all of my WPF controls to have have automatic height and width, and I set the alignment to stretch. That works great in a lot of cases. My windows come up in a reasonable size by default. If the user stretches the window, the extra space will go where I think it should.
This broke when I set the width of a DataGridTextColumn to "*". Now suddenly the preferred size for my table is several times bigger than my screen! The initial size for my window is exactly the size of my screen.
Of course, I could set the MaxWidth of my table (or my column), but I don't want to do that. The max width was fine at infinity.
I tried changing the Width property to a fixed number of pixels. That seems to change the maximum width. Even though I set HorizontalContentAlignment="Stretch" in the table, the table is centered and the width does not change.
Is there a way around this? I like auto-sizes most of the time. And I like setting the width of a column to "*". Is there a way to make them work together?
It seems like there's a property somewhere that I can't find. At some point the container has to ask my table what size it wants to be. That answer might be ignored or adjusted, but the table had to answer that question. It seems like I should be able to change that value, without changing the max size. (And it seems like if I say "stretch" and there is extra space, my table should be stretched.)
Thank you.

Related

How do I calculate the required size for a CheckedListBox?

I have written some code to automatically scale a CheckedListBox to its contents using mListBox.ItemHeight and mListBox.CreateGraphics().MeasureString(...).
The output from the string measurements is a bit dubious but what really puzzles me is how much to add for borders and such.
I tried both SystemInformation.Border3DSize (= 2) as well as the difference between ClientSize and Size (= 4).
But taking mListBox.ItemHeight * mListBox.Items.Count + 4 for the height makes it one pixel too small and a scrollbar appears.
For the width it does not work at all because it does not take the size of the checkboxes into account for which I can't seem to find a source.
How should I determine the size of the control?
In cases like this, it is typically easier to set the ClientSize rather than the whole Size. One thing to note about ItemHeight is that it does not include the margins of the item. Using a CheckedListBox with default settings, I had an ItemHeight of 13. But the ItemRectangle property had a height of 15.
So here is what I did. I added 9 items to the CheckedListBox (the first Item was longer than the rest), keeping the default size of the control as set by the designer. Then in the constructor of the form, I set the ClientSize like so:
this.checkedListBox1.ClientSize = new Size(TextRenderer.MeasureText(checkedListBox1.Items[0].ToString(), checkedListBox1.Font).Width + 20, checkedListBox1.GetItemRectangle(0).Height * checkedListBox1.Items.Count);
Notice I used TextRenderer.MeasureText to measure the text. It will typically give you better values than Graphics.MeasureString. By default, TextRenderer included a bit of padding in it's measurement. I also included a 20 pixel padding to account for the checkbox. Anyway, with TextRenderer.MeasureText, and the 20 pixel padding for width, and ItemRectangle * Items.Count for the height, that gave me a CheckedListBox that was sized to its contents without ScrollBars.
EDIT: If item widths vary, it may complicate setting the width, as you'll want to set the width based on the longest item. In this case you'll need to measure each item and keep track of which one was the longest.
EDIT 2: Ok so I dug around in the CheckedListBox source code. They use a default size of 13 plus a 3 pixel padding for the width and height of the checkbox. But, if VisualStyles is enabled, they call CheckBoxRenderer.GetGlyphSize to get the size to use because it takes into account the DPI settings. Most machines are set at 96 DPI so that method will still return a size of (13,13). So when you're measuring the text of the item, you can also pass the Graphics object and a CheckBoxState enum to the CheckBoxRenderer.GetGlyphSize to get a size. I used System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal for the state, but I'm not sure that the state matters. I tried CheckBoxState.MixedDisabled as well and it returned the same size, (13,13).
So to summarize, you can use CheckBoxRenderer to get the size of the checkbox, but you will still probably need to use some padding. This reduces the need for hardcoding a magic number to account for the checkbox. Since the checkbox is drawn and isn't an actual control, its size can't be determined like sizes of controls can.
Here is a link to the source of CheckedListBox. It wouldn't hurt to look at it. Specifically, look at the protected override void OnDrawItem method.
CheckedListBox Source

Auto resize DataGrid column to content

When column width is set to auto in DataGrid, the column only seem to be able to grow, never shrink in size.
I would like the column to always resize to the actual content of the column.
My question is similar to this one.
Except I would like the resizing to happens automatically when content changes, without having to explicit execute some code.
Also I would like it to be flicker free and fast or if I'm asking the impossible, then almost flicker free and fast at least :-)
I found a link which seems helpful, http://forums.silverlight.net/t/71184.aspx. But I can't find DesiredWidth property in the code...

Smaller PanoramaItems in a Windows Phone 7 app

Is it possible to decrease the default width of a Panorama control's PanoramaItems?
They can be made wider by specifying Orientation="Horizontal" and a width larger than 432. However, specifying a width less than 432 does not result in a smaller PanoramaItem. It's minimum width is 432 always.
I played around with the templates but couldn't figure out how to achieve this.
I'm afraid there isn't anything you can do about that. The Panorama has the width of its items baked in as 48 pixels less that its own width.
PanoramaItems are not designed to have a smaller width. If you wanted to do this you'd probably need to create your own version of the Panorama control.
Don't do this though!
Not only would it be a lot of effort to get right you'd break user expectations about how the panorama would work.
Would/could you allow multiple items to be displayed at once?
What would be the impact on navigation?
How would you communicate to users that your app works slightly differently to all the other ones on the phone?
How would you justify the mental cost to the user of the different behaviour?
What would the impact be on the item headers if they were smaller?
With the PanoramaItem width set to at least (almost) the width of the screen it allows the user to see and focus on a single item at a time.
Also, potentially having fewer items on screen at once can also help with performance too.

Why isn't always possible to set a winforms dimensions?

I'm trying to size a checkboxlist control to 500,250 from its current 502,251 (It's an OCD thing) but every time I try, it just reverts to 502,251.
Is it because the parent container is docked in the window? Are there any workarounds?
(This is through the visual designer)
Most likely the control is being resized due to the control's font size. The ListControl does not like to display an item that will be "split" by the bottom edge, so it will resize the height. Try changing the control's font size and adjust again to verify.
No work around, and you really do not what one, because the control is really doing the right thing.
Yes, it is OCD. I have it also, but this one you have to let go. :O) Consider yourself lucky because you are only one or two pixels off. I was five pixels off once, and I had to put a note on my monitor to ignore it. It so bothered me.

MaxAutoWidth in WPF

Is there a good way to limit the Auto Width of an element or Grid Column?
I want to limit the Auto Width only, but allow any user changes.
For example, I want to allow GridSplitter to arbitrarily resize the column, but the initial (Auto) Width should not exceed a specific value. Same for the elements -- I want to allow resizing, but limit the Width they have if never resized.
MaxWidth works exactly as opposite -- it does not affect Auto Width of Grid Columns (it might be a bug), but limits the resized width.
Is there any good solution that works for (almost) any element or Grid part?
Try binding the MaxWidth to the Width and use a IValueConverter to react to changes from Auto to specific values.
In the grid column set the width you want the column to start at. Any changes to the column width made by the user using the grid splitter will overwrite the width you start with.

Resources