I want a grid in my page to have a single column in portrait mode and have two columns in Landscape mode. Some of the content should move to the second column when the device is Landscape. I've tried to do this but couldn't.
I'm doing everything from code behind. Generating the grid, adding children etc. When Orientation changes, I destroy the current layout and create a new one. The problem with this approach is, any entered data will be gone. It's a huge code and not possible to put here. I want this layout change to happen automatically. So, any data that is entered is preserved after the orientation change.
Any help would be appreciated.
The trick is to have one grid that accommodates both states. You can use VisualStates to assign different Grid.Row, Grid.Column, Grid.RowSpan and Grid.ColumnSpan values to controls. This is done using Blend and is declarative in XAML. you give each state a name. In the code you detect size change of the window to trigger the different states using the VisualStateManager.
You can also do this without VisualStates. In that case in the event handler of the SizeChanged you have to set the appropriate values for all the controls yourself.
In Windows 10 UWP it got much simpler using RelativePanel. You can position controls in a more flexible way then with grids.
Martin
Related
I have a chart with multiple chart areas and I want to have a listview where the user checks different signals, that are being plotted in the areas.
Since I want some signals to be displayed in more than one chart areas, I need the listview to reset every time the user selects a different chart Area.
To achieve this I think it is a good idea to create a new listview every time the user selects another chart area and bring the new to front.
My question is , how can I create multiple listviews at the exact same location, and also if someone has a better idea to this.
As you know, you can define the listview in code and do all the styling by hand or you can create a customControl for that.
But then, when the load event of your main form is fired, you create all the listview/customListViewObjects that you need and place them in the same location with the same size by setting their Location and Size property. Finally, you can use BrintToFront() on the required listview when an area is selected.
There are some other ways, but they aren't beautiful in winforms.
A) you can create a tab control with N tabs, then place a listview in each tab and set the tabControl style to the tab header/title is not shown. But as far as I remember, the tabControl will add a border frame that your cannot get rid of.
B) If you don't have many areas and its number is not going to change, you can create the listviews manually in your form editor, and place them wherever in your form (with the appropriate parent, of course). Then in your load event, you can set their location and size properties to the one you need. This is kind of ugly and you may see the controls moving when the form is loaded if you are not careful.
On the other hand, you can just have one listview and then, when the area is clicked you can reload all its child controls for the different signals.
I have the following application:
I am developing a Windows Store app in which I need to show a big grid filled with buttons. The content of the buttons are some numbers and when I click any of them, I open a Popup with editor, where I edit those numbers. For that purpose I use a GridView, I put an ItemsWrapGrid as ItemsPanel. This makes the grid look exactly as I need it to look. I put the GridView inside a ScrollViewer, because I need to scroll the grid in both directions, since it has a lot of elements. Also I need to have the pinch-to-zoom effect that the ScrollViewer gives out of the box. I need to change the ItemsSource for that GridView when the user chooses different source in a ListView next to the GridView.
The problem:
Putting the GridView inside the ScrollViewer breaks the Virtualization inside and this has a major impact on my performance. When I switch the ItemsSource of the GridView, it takes more than 3-4 seconds for the rendering and during that time, the UI is frozen.
My question:
How can I keep the awesome stuff that the ScrollViewer gives me and also keep the virtualization? Because when I remove the ScrollViewer, changing between the ItemsSources happens almost instantly.
Any idea?
You'll be best off implementing virtualization yourself since you're trying to use the GridView far from the use cases it was designed for.
Put a Canvas in a ScrollViewer that does both pan and zoom and handle view change events on the ScrollViewer by laying out item containers inside and around the viewport. Avoid unnecessary changes too, so keep containers in place if they are to stay realized between view change events and recycle containers that are leaving the viewport neighborhood.
I am attempting to insert a panel into my WPF application that would have a few very specific behaviours:
1.) Wraps content evenly. Starting from the top left corner and running downward, before moving to the next column.
2.) Allows me to define a maximum number of columns to wrap to. For my purposes, this number would be between 1 and 3.
3.) Allows me to set an initial height, but it will also grow to accommodate additional items. (Only setting an initial height because my content won't wrap without it. If I leave it auto, it all comes out in a single column regardless of whether it fits on screen or not)
At this point, I have concluded that what I'm attempting do will require a custom panel, but I'd like to ensure before I begin that process (and learning how to do so) that I'm not missing a much simpler answer.
A WrapPanel can be set to wrap vertically, but you have no control over the number of columns.
A UniformGrid would offer you control over the number of columns, but wraps horizontally not vertically.
In short: you need a custom panel. The built-in ones do not offer the combination of features that you want.
UniformGrid has a LayoutTransform property, which can be used to transform it in order to change the position/rotation of the elements inside. But it will also transform the content.
Some more tricks involving Setters on the types of the items inside your UniformGrid and the content can be transformed again to retain the desired "original" orientation.
You can learn more in this tutorial.
Alternatively, it seems that the Extended WPF Toolkit contains its own implementation of UniformGrid, with an Orientation property, the only problem being that it won't grow to accomodate the number of items; instead, it will obey to an arbitrary Columns property.
Then again, you may be able to change the value of this property each time you add a new item/resize your UniformGrid, but it will be some more manual work and may potentially lead to code behind, which could be seen as an issue if you're working in MVVM.
Just ran into a bunch of random but probably very simple questions while learning to work with WPF. If anyone can answer any of these it would be most appreciated.
I have a Rectangle that I styled to my liking, and then a StackPanel that I am actually placing the content (bunch of labels) in. It looks like Rectangle doesn't have a closing statement so how can I make it the parent of the StackPanel (I want the panel to move with it)?
I need to display text (labels) in a table form. It will only be two columns, and 8 rows (row count may change). I initially looked into just using a Grid, but as this is a intended for Layouts, it didn't work as expected. I also checked out DataGrid, but this seems confusing and overly complicated for my needs. Is there any simple Table style element I can use?
I have some Animated Expanders and I want to give the other UI content elements in my application a similar looking title bar. I haven't seen any sort of element for this, so I am wondering if I just need to use a Label or something and style it as close as possible to match, or if something already exists that I should use instead. If I need to use a label, what is the proper way to group/attach it with the element that it is the title bar for?
Do any controls exist for WPF that would allow for a Mac style menu bar? Something similar to where the icons move and expand as they are mouse over.
I have a grid that has two expanders (one above the other) with Height="Auto", and two rows set to Height="Auto" so that when the top is minimized, the bottom one moves up to close the gap. I am using a MinHeight right now to make sure they are at least somewhat expanded, but I would like to make them take up the full 100% of the Grid height. What should I do for this?
A rectangle cannot have child content, a better approach might be to use a Border, you can use this as that parent of your stackpanel, applying a Background and BorderBrush to make it look like a rectangle.
For a simple table layout Grid is the way to go. Is your problem that you need it to be dynamic? If so, you can create multiple grids with SharedSizeGroup on the columns to make them look like a single grid. See: How to align separate Grids created via templates along their columns / rows?
Learn about creating your own templates!
Not that I know of, but it would be relatively simple to do, create a storyboard that scales the item on mouse over.
No idea ... running out of steam!
As Andrzej Nosal mentioned, these really should be separate questions!
hii
I am a fresher in the c # so i want to know how to resize the datagrid(any other control)aith respect to the form size.
I just added one datagrid in the form then what i have to do?please help me...from the very basic please
For a dynamic layout that scales with your form size you have various options (depending on the complexity of your layout).
The first ones are
Anchoring
Docking.
But you can also work with advanced container controls like
TableLayoutPanel
FlowLayoutPanel
SplitContainer.
Some more informations i already post in an older question.
If you have more concrete problems about how to solve a specific layout problem you should post a new question with your exact problem.
But at a first tip i can say that it is never necessary to use the Resize event and do some size changes on yourself. There is always a solution that can be solved with the above elements.
You have to ways of dong it:
Using a Dock. It works fine and is very easy to use but its limit is that you can stick it to a one side of parent container. So if you want to streach control only in width you will fail.
Using Anchor. It require more configuration but you can specify all four(top, right, down, bottom option separably.
Regards
Szymon
Go to the properties window and scroll down to "Docking" and choose to dock the control in the parent container. This will give you various options about how you want the control to dock. You should put some containers in there of some sort, maybe, if you've got buttons or things you'd like to have show up above/below/next to the grid. Also, if your grid goes behind your other controls, select the control and bring it to the front.
edit:
You need some containers in which to put your buttons / drop-downs. You could use a flow-layout panel (which wouldn't resize its child controls), or you could use a plain panel or the table-layout panel. The table one will let you dock your child controls within each cell of the table, and you can set your columns & rows to auto-size to a percentage of the entire table width. That way everything will autosize accordingly.