How to get WPF usercontrol designheight and designwidth at run time - c#

I need to access a WPF usercontrol's designheight and designwidth at runtime. I know this is impossible, so I'll explain why I want to do it, and hopefully someone can provide an example of the right approach.
From a C# winforms app, I open a new window containing a very large WPF usercontrol. If the monitor I'm running on can handle it, I want it to open the window at the full design size of the usercontrol. If the monitor I'm running on can't handle it, then I want to open the window as large as possible, leaving 1 pixel on each side of the screen. So I need to know the usercontrol's current design width and height before the usercontrol is displayed.
I want to avoid hard coding values in the winforms app, or using unbound public properties in the user control, because then I have to trust other developers to update them when they change the user control. I don't know if it's possible to bind a local variable to designheight or designwidth, or how to code that in XAML. Since I don't know a lot of WPF, there may be some other way to reach the goal that I don't know about.
Thanks for your help and advice.

Why don't you try something like Resize WPF Window and contents depening on screen resolution
The answer suggests that you bind the screen size to the window height and width. It will automatically size the window according to screen size. You can even set a % value for the size of the window.

Related

Scale WindowsForm

Basically, I want everything (Controls etc) to scale with the WindowsForm when resizing it by dragging, so that the user can determine the size of the UI himself.
The picture is not perfect, but I hope it explains it:
The easiest way to do this would be to use a (Flow/Table)LayoutPanel and the appropriate Anchor/Drop properties, but I feel like that restricts my design, which currently looks like this:
My idea was to scale all the components when Resize() is called:
foreach(Control c in Controls){
c.Scale(scaleFactor);
}
My problems are: Locations aren't set properly and the rounding needed to determine the 'scaleFactor' leads to inconsistencies.
Is there a clean way to do all this? Do I have to use LayoutPanels to get a clean way?
Are you sure that you want to zoom those text boxes and labels, too? Text boxes have a fairly standard height, for example. What do you expect if the window is so small that the text is too large for the buttons/text boxes, etc.?
The typical resizing logic of the contents of a resizable window is a little bit different. I would rather create a borderless panel for the groupboxes and stack/dock everything like this:
If I resize this window, that will look like this:
Btw, I do not like if buttons are resized like this. I would use only Anchor = Left, Right for them so they would preserve their height. And do not forget to set a proper MinimumSize for the form.
Simplest and cleanest way is use layout panles.
It's hard to code location of controls which depends of previous control's location and size (expect case when you know order of controls).
In WPF everything are on ContentControl which is layout panel. Location of control is calculated automatically.
Another problem might be scaling font or glyph icon in combobox. In WPF this simplest and supported.

Windows Forms: Auto Scale Application

I have a WinForms Application that was designed to support Full HD resolutions (so 1920x1080). Now this App is also supposed to run on a lower resolution: 1600x900.
Is there a way to let the application auto scale itself to fit the lower resolution? Like you would just downscale an image, basically resizing and relocating each control.
My current forms and panels have set their size to 1900x1080, so they just extend out of the screen on the lower resolution.
I have played around with AutoScaleModes and AutoSize, but the best I could get were Scrollbars so that you at least navigate through the forms. Is such a thing as downscaling an application even possible (retaining dimensions/ relative sizes and positions of the controls)?
Thanks in advance for any inputs on this!
If your main form starts in a maximized mode, it will adjust its size automagically.
But (and this is a huge "but" according to your question): the inner controls won't be scaled as you would see on a smartphone. WinForm is not "vector based" as WPF. If you have a fully loaded form in 1920x1080, when the main form is sized down, the controls won't fit and you will get scrollbars.
So the answer is: No.
The solution is available.
Form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
and
Make all control autosize = false.
make a suitable size of your wish.
The property you are looking for is called Dock, it's a property of the controls.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock(v=vs.110).aspx
This basically tells the control how to fill the available space.
Creating a resolution independent application is not a simple logic. Everything in the window should be resize as per the selected resolution. Not only controls' size you have to change the font size also as per the changes. Here is the example how to create a resolution independent form using C# code. Another way is use DevExpress Tool. This tool provides Layout Countrol Container. You can place each control in separate layout item and assign the minimum and maximum size of control.

Resolution for full screen WPF application

Following the guide here, I have created a full-screen WPF application. But I met a problem: the various size & resolution of screens. For example, I want to put several sprites on the screen as buttons; but they are located at different positions in each screen, and even different to what shown in the XAML designer.
I have searched all over without a clue got. How can I fix this problem? (to make the buttons appears the exact place (in the center), and better, help the xaml designer reflect exactly what will happens when the program is running). Any help will be appreciated.
UPDATE: I'm defining my page as a Canvas inside the Window element. Actually I like Canvas more, cause I can easily put my sprites anywhere, not like a grid.
In general, you should not use pixel values in WPF.
Instead, you should layout your content in <Grid>s with rows and columns, and it will automatically expand to fill the screen (based on the alignments and row / column definitions).
Avoid using the canvas. Also, do not rely too much on the designer to build your layout. Using Grids, Stackpanels and/or Dockpanels will give much better results (and scale when resizing your window). For example, if you use only the designer and drag-and-dropp all your elements, the designer often puts huge margins a bit randomly and this will not always scale properly if you resize your window.

Graphical hiccups in C# User Control - Resize obscures components

I'm experiencing difficulty with a custom-made User Control, and my searching on Stack Overflow, MSDN, and Google didn't pop up any troubles quite like the one I'm experiencing.
I have a very simple User Control: It's a label, a text box, and a button, with a SaveFileDialog and a FolderSelectDialog available. The text box and button are anchored Left,Right and Right respectively, with the intent that if the control is resized larger, the text box will enlarge to fill the gap, and the button will stay on the right edge of the control.
The problem I am encountering is that when the control is enlarged, the area to the right of the default width of the control becomes blank space when the project is built and run. The pictures here will illustrate what I mean:
In editor:
Running:
The control is smallish in its design window, but when I add it to a form and widen it, it behaves as intended. However, when I run the form the control was added to, half the control isn't visible.
I suspect that I'm overlooking something fairly straightforward, but I wasn't able to find anything addressing this point in my search. Help would be much appreciated.
My guess is that there is a panel or something that is added to your control and will be brought to front somehow runtime.
from property window's top there's a combo from which you can select all the controls in your User Control.
check if all the controls are what you want.
if you find that panel or anything delete it :)
EDIT:
alright this was not your problem.
now I can only assume that you have set some manual sizes to your user control, i.e. in its constructor. in that case designer will show the correct size of you user control,
now some other place in your code, you have set the user controls size manually again. if the layout is suspended and size changes, I think that the anchored controls' size will not change automatically.
if this is your problem, it is probably hard to find.

Looking for a good WPF solution for a transparent, click-through overlay

I want to try something different, and am attempting to display an overlay on top of my current WPF GUI that allows the user to still interact with the GUI, but provides a layer of annoyance to let them know that something's up.
My question is really two separate questions:
1. How would you do the overlay?
My first attempt was to use a Rectangle, set the Fill to the appropriate color, and then change the Opacity. But it's not transparent to click-throughs. So I think what I want to do, according to search results, is to create a separate window, set its Background to Transparent, and then set AllowsTransparency to True. While this works, if I want to do something like Background="DarkRed" Opacity="0.2", click-throughs no longer work.
And this leads me to the second part:
2. What's the right way to resize this overlay region if I'm using MVVM?
My main window creates the ViewModel, which creates the Model. The Model is the only thing that knows about whether or not the overlay should be displayed. But the main window obviously is the only thing that knows its size, and the Model never knows about anything above it. Is the only way to achieve this to databind the overlay Window's size to properties in the ViewModel, and then have the ViewModel set these values any time the main Window's size changes?
Anyone have clues on how I can achieve all of these things?
To address part 1: set IsHitTestVisible="False" on your overlay, be it a Rectangle, Border or some other element. This will allow you to make it whatever color and transparency level you want without affecting interaction with the underlying controls.
Part 2: you shouldn't use a separate Window. If you keep the overlay in the main Window you can rely on the layout in your view to automatically handle sizing of the overlay.

Categories