XAML: How to set controls properties depending on screen resolution? - wpf

I'm developing WPF application (.NET 4) where I have few UserControl's which looks pretty good on most of the screens.
But now customer have new monitors where some elements are too small. The best solution we found is to make some elements smaller, to left more space for the main panel.
The layout is now pretty complex, and I spend a lot of time to find which margins, heights, widths and so on.. I need to change to implement this.
Usually it is some children elements Height, Width +-10 or so. Sometimes Margin. And in one case it's Style's Setter Property="Width" which defined in Resources.
I'm wondering is it possible to make this changes configurable, so for my specified screen resolution (let's say I know only that Width=X and Height=Y) they was applied, and for all other screens it stays the same as now?
How to do this using as less code-behind as possible?
I like the solution with VisualStateManager and VisualState.SateTriggers, but looks like my application does not recognize this.

When you are using user controls inside the window may be you could set the height and width of the grid's row and column definition to '*' and also make scroll bar visibility of horizontal and vertical scroll bar to auto.
Hope it helps you.

Related

Scale usercontrol in wpf ? vertical and horizontal scrollbars

I implemented a usercontrol, but i have a problem with rescaling the window. I know when i make the window smaller, everything scales, and every textbox and label becomes also smaller. But this is not what i want, i just want that when i make the screen smaller, everything stays the same size, and that scrollbars appear ( vertical and horizontal ). How do i do that?
Thanks
Assumption
The behaviour you describe, is not mandatory the default layout-behaviour of WPF. It depends on the layout-controls you use. I assume, you're using a Grid with setting it's columns and rows to the Start (*)-GridLengths. This would have more or less such an effect as you describe (without scaling). Or maybe you are using a ViewBox, this control scales the whole content based on the available layout size.
Solution
I guess that wrapping your whole content into a ScrollViewer will probably do what you desire. If not, I suggest that you post some XAML-code to show us how you have built your content.
<ScrollViewer>
<YourContent>
</YourContent>
</ScrollViewer>
Update
If you really scale your window (applying a ScaleTransformation) and you want your UserControl to be the only control within that does not scale, you have to scale your UserControl in the opposite direction as you have done it with your window. Apply a ScaleTransformation and set the scale values to 1/scale. Or try to use the ViewBox to blow up the content of your UserControl, but this will not be very exact.

WPF, any easy way to work with different screen resolutions?

Given a WPF Application running full screen, a fair amount of controls some of which will animate from off screen to center. I was wondering if there are any special ways to save on the amount of time required to optimize an application for different screen resolutions?
For example, using Blend I've setup some text, which is originally off screen to scroll into view. Now in design mode the start positions are static. If resolution changes the start positions will obviously not be correct.
So I guess to correct this, during app startup. I need to
Check resolution
Move the text box to the desired start location
Adjust the storyboard as required, so the frames all have correct co-ordinates depending on the res of the screen.
I don't mind doing all of this, but if there is an easier way please share!
Thanks
In WPF layout of controls should be made in such way, that when size of window or content changes, controls automaticaly resize/reposition themselves to reflect this change.
This is highly affected how your layout is made, especialy by using specific panels, that do layout of their child elements.
This is also made obvious by using device-independent units for sizes, margins and sometimes positions of controls. And also allows different kind of zooming and scaling of whole UI without any need to redesign the whole thing.
If you are trying to position your controls absolutely, either by using Canvas panel or margins, your are doing it totaly wrong.
In WPF, scene is measured in abstract units, not pixels, and controls are freely scaled. There should be no problems to center something, or what?

Setting control height explicitly

I have a XamDataGrid in one of my user controls, inside of a stackpanel. I want the grid to maintain the same height regardless of how many rows are present in the grid. To do that, I set the grid's Height property to an explicit value.
Is that how things are done in WPF? Every time I do explicit sizing I feel like I am doing WinForms and not using WPF properly. Is setting the Height directly the only/correct solution?
There's nothing wrong with setting an explicit Height in situations where you want an element to always stay the same height. Where it's less appropriate is in situations where sizing is better handled by the parent layout Panel or the element's child content which can use the available space dynamically.
WPF uses a relative measurement system which at first glance is not intuitive. I have never found an example when I was forced to use explicit sizes ( once when I paint something on Canvas). I use styles in 90% cases where I define Padding, Margin, Aligment etc. Sometimes I use MinHeight and MinWidth for simple things.
About that Grid you can put it in the ScrollViewer or ViewBox to have dynamic sizing, yet If it won't be trouble set the explicit Height.

WPF - UserControl sizing on Custom Canvas

I have a custom canvas that I can drop controls on, move, and resize. I need to be able to at least start with a pre-determined size but then allow re-sizing.
My problem is that I have a user control that doesn't appear to resize. I set Height and Width on the user control (works on drop). But then as the control is resized on the canvas the visual size stays the same (the sizing handles are changing). I can see the Height property changing (usercontrol.height) as the control is resized. But again, the appearance of the control stays the same size.
I had a thought that the inner container on the user control should be bound to the usercontrol.height but that didn't seem to help. (or maybe I didn't have the binding right).
On the same canvas, if the dropped control is for example, an Image control, I can set an explicit height on the drop and everything resizes as it should. So it seems to be UserControl thing.
So is there something special I need to do with UserControls to get the visuals to resize? Should I be using explicit height and width to get it all started?
Thanks for any help.
The WPF layout system can be confusing, as you've discovered. There is no substitute for a solid understanding of the layout and measurement system. If you plan on doing much WPF development, it's worth taking 20 minutes to read this article on the layout system and then experimenting for a while in an iterative environment such as Kaxaml until you feel comfortable.
The system is very logical. It won't take long for the penny to drop. Sorry for the indirect answer, but I don't believe there's enough information in your question to really explain what's going on.
You might also look at the HorizontalAlignment and VerticalAlignment properties.
Not a WPF expert but I believe you need to enable auto-sizing in order to achieve the scenario you are looking for. This is done by setting the Height/Width of a control to Double.NaN which essentially says, "I have no specific size". Once you do that, the control should resize to occupy available space based on the need of the control.

Auto-size controls in .NET CF to avoid horizontal scrolling?

I am developing a form in .NET Compact Framework, which hosts a variable number of controls. Every control should have the same width as the form. When there are only a few controls, no vertical scrollbar appears. When there are more controls than they can fit in one form, a vertical scrollbar appears. The width of the controls should then be modified, so that no horizontal scrollbar appears.
What is the best way to achieve this? I am interested in a solution that will work in all platforms/screen sizes and that can support screen orientation changes.
If I get this right, at one point, both a vertical and horizontal scrollbar appear, and you want only the vertical scrollbar? Doesn't setting the Anchor of each control to "Top|Left|Right" solve this problem automatically?
If every control is to be the same width as the form, why not just Dock every control to Top (or Bottom)? It'll take care of the resizing for you then. It might not look very attractive however, so I suggest adding in some empty Panels (docked the same way) to be used as vertical spacers.
I did some quick testing, and it seems, when you add controls, the panel raises the resize event when the added control tiggers the scollbars to go visible. The annoying part is here that the resize event is triggered a couple of times during startup :(
But knowing the compact framework, this might be your best shot at handling this.
Normally on the full framework you could if the DisplayRectangle is bigger than the size of the panel, but no such thing exists on the cf.
Hope this is of some help, I'll see if I can find anything more in the morning.

Resources