WPF Application Maximize Problem - wpf

How to set the Height and width of a WPF application on maximise?
Problem I face is because of variable height of the windows taskbar in different computers
Presently, I am doing it like this. Please suggest any better technique
if (this.WindowState == WindowState.Maximized)
{
this.Height = primaryScreenHeight - 10 - System.Windows.Forms.SystemInformation.SizingBorderWidth;
this.Width = primaryScreenWidth + 2 * System.Windows.Forms.SystemInformation.FrameBorderSize.Width;
}

You dont need to do this. If you use a Grid as your layout container, objects within will always expand to use up all the space. Then you can use Margins to arrange sub-panels. Using the various HorizontalAlignment values of Left,Center,Right,Stretch and VerticalAlignment values of Top, Center, Bottom, Stretch, you can make pretty much any scalable layout without having to check if the app is maximized/minimized, etc.
Is there are a particular layout you want to achieve that perhaps I can post an example for?

Related

Scroll bar slider behavior

I am implementing a scrolled window control.
I need to draw simple scrollbar sliders for this purpose.
Usually, sliders shrink as the content grows, but I am unable to understand how.
What is the relation/ratio formula? I've made several google tries to find out, but I didn't.
Does it have a minimum size it can go or it is just really hard to shrink it that far due to exponential shrinking behavior? We are talking simple generic scrollbars. What would be the proper way to implement the proper slider dynamic sizing?
Obviously this question is so dumb that nobody would bother answering.
I used to make complex hacks but now the simplest logic just slipped away across my mind.
The horizontal scroll bar slider width is the portion of the content (child) width.
(width / content) * width
width * width / content

Codename One app shown in a centered rectangle on wide screens

Note for the readers: this question is referred exclusively to Codename One.
Because I have multiple targets for the same app (Android, iOS, Javascript), that can be run on small screens (that is the typical use case) but also on wide screens, I have to do a choice about the UI.
I want to simplify my life: mobile devices held on portrait mode are my primary target, so... all wide screens (desktop and tablet) will show the app inside a vertical rectangle in the center of the screen. Maybe this is not the best solution, but it can make sense. I suppose that the width of that centered rectangle is 9 cm (my iPhone device width is about 7 cm and my Android device width is about 8 cm).
So... what is a proper way to accomplish this requirement?
Initially I thought that I can create a BaseForm with a code that override the show() method, setting a left and right margin for ContentPane, LayeredPane and Toolbar before calling super.show()... but this solution seems to me a poor way to go and also an overhead, because in that case I'll need to recalculate the margins and to revalidate the BaseForm for every change of size of screen... and, however, this solution will not work with FABs or Dialogs. So... it's not a solution.
It could be nice if I can choose a background (an image or a color) for all the space outside the centered rectangle containing the app.
Any better idea?
This is probably something that we need to implement in the system to make it seamless. Any approach you choose will require some work.
If I were doing something like this I'd create a special form with no title that acts as your background. I'd give it a special custom layout manager that centers the content. Something like:
public CenterOneCompnentLayout class Layout {
public void layoutContainer(Container parent) {
if(parent.getComponentCount() == 1) {
Component cmp = parent.getComponentAt(0);
cmp.setWidth(convertToPixels(9));
cmp.setHeight(parent.getHeight());
cmp.setY(0);
cmp.setX(parent.getWidth() / 2 - cmp.getWidth() / 2);
}
}
public Dimension getPreferredSize(Container parent) {
return new Dimension(100, 100);
}
}
Now just add the actual form to this background form. The rest should work "as is". I think you would need to guard against some things and it's possible this hack will produce some side effects due to the nesting of the forms. But ultimately it should work.

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

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.

ExtJS 4 desktop taskbar - how to get height programmatically

It would be helpful to get the taskbar's height programmatically when sizing windows and preventing them from covering the taskbar. I can hardcode the height in window size calculations for now, but would appreciate a better approach.
I ended up not needing the taskbar height value anymore, because there is a way to get the desktop height without the taskbar:
_myDesktopApp.desktop.body.dom.clientHeight
Before, I was using:
_myDesktopApp.desktop.getHeight()
Which gave me the combined height.
So to get the taskbar, if I ever need it, I can do:
_myDesktopApp.desktop.getHeight() - _myDesktopApp.desktop.body.dom.clientHeight

Is there a way to automagically make a canvas scroll on overflow in WPF?

Been checking the web and this site, but couldn't come up with any descent results.
Is there a way to make a canvas in WPF show scrollbars on overflow ? Been trying the scrollviewer, but can't get it to work :(
Thanks in advance..
The problem you're running into is that Canvas, unlike many WPF panels and containers, does not size to contents. That means if you add an element which goes outside the canvas boundaries it will not update it's size. Hence embedding a Canvas in a ScrollViewer will do no good unless you manually update the size of the Canvas.
It sounds like what you want is a Canvas which supports size to contents. This blog entry has exactly that control.
http://themechanicalbride.blogspot.com/2008/11/auto-sizing-canvas-for-silverlight-and.html
I took a different approach and abandoned the Canvas for Grid. The Canvas is more performant but for my purposes at least I haven't noticed a difference. The grid can mimic the behavior of canvas by doing the following.
Create a single row,single column grid.
Set the HorizontalAlignment to Left
Set the VerticalAlignment to Top
Use Margin "x,y,0,0" to set the position.
Bam..works just like canvas and it works great in a Scrollviewer.

Resources