Grid for 2x2 table in Canvas? - c#

I am new to WPF. I want to draw a simple 2 rows by 2 columns table inside a Canvas. Can I use a 2x2 Grid to do this? One obvious problem is that the Grid stays 'invisible' in the Canvas, probably because it is meant to hold other visual controls ( instead of being visible itself, which is what I want). If Grid is a bad idea for this 2x2 table, please advise the correct method.

The grid panel would be a quick way to draw this. In order to give the visual appearance of a grid I would recommend putting a border inside each one of the cells and setting its borderthickness and borderbrush.
There are many other ways to accomplish this but for something this simple the above method should suffice.

Related

C# Windows Form - Anchor

I have two elements in the form, two GridViews.
Both of them are aligned horizontally at the same level.
When my form is resizing, I want both of them to change their width/ height according to the form size.
The problem is, if I try to anchor them, they kinda overlap at some point and I want to avoid that.
My question is, can I anchor one Grid View to the other Grid View and not to the form?
A solution can be a TabelLayoutLabel, but i don't really want to use this.
My question is, can I anchor one Grid View to the other Grid View and not to the form?
no you cannot, and if you could that would be bad practice.
the anchor property description by microsoft:
Gets or sets the edges of the container to which a control is bound
and determines how a control is resized with its parent.
a dataGridView is not a container it's a control.
it sounds like you should use a table layout panel, that would give you the best results.
search for it in the Toolbox:
make sure you set its property to Dock = fill, where ever you want, and
,
next. in the property of the table view, you enter Edit Rows And Columns
and set each column to the percentage that you desire
add your grids to each column,
and set each one's 'Dock' property to Fill, and there you go
you can undock the tableLayoutPanel and change its size,
Hope that was helpful

Visual studio, fill space

In my form I put three charts. I want to fill all available space, and tried to set dock fill, but in this case the charts overlaps each other. I want, instead, to have every chart next to others.
How can I fill all the space, without overlaps?
As mentioned in the comments you want to use a FlowLayoutPanel or TableLayoutPanel. Below are two examples of TableLayoutPanel which may be better because you can more easily control the desired layout.
The examples below use panels instead of grids but the idea is the same for whatever control you want to put inside the TableLayoutPanel.
In both examples, the Dock property of the TableLayoutPanel and all 3 components is set to Fill. This will cause everything to resize automatically as the form resized. Additionally, there is a Rows property and Columns property on the TableLayoutPanel which will allow you to set either pixels are percentages of the table a cell should consume.
Example 1: 3 panels side by side
Example 2: 2 panels above a third panel. In this case you set the ColumnSpan property to 2 on Panel 3

placing objects like rectangles on a canvas

I started my app by placing rectangles and other objects on a stackpanel. That worked will until I wanted to split my rectangles and have two columns of rectangles. The vertical stackpanel worked well until I needed to split my rectangles into two columns and put stuff on the left and stuff on the right.
So I converted to a canvas. Now
mainCanvas.Children.Add(grid); // seems to replace what the last Add placed on screen. Any ideas how to control the position of items added to the canvas?
Edit:
Ok clearly a canvas is the wrong panel to use. Two column's of stackpanel's might be made to work. But when I look at what I am actually trying to accomplish, the column's aren't limited to two, but could be n number of columns. Why? Because the application is a flowchart style app that builds a custom language script. The diamond decision shape splits one column into two which can split into two more extra.
I wonder about using a single stack panel and just making a grid with multiple grids horizontally in it. But mouse events would have to be smart enough to know which grid in the grid your actually in. Not undoable I think, but not trivial...just looking to see if there is a obvious use this choice that i am missing by being a wpf rookie.
Edit2:
The issue I have with just using a grid is that when the window is stretched a
grid does not resize to the new window size.
making a grid of multiple other grids, then using isMouseOver to test to see which grid in the single child of the stackpanel actually needs mouse highlighting works.

Place control over other

I am searching a way to put a ComboBox over another Control with xaml. The new combobox should aligned on the right side of the window.
How can I do this?
Best Regards, Thomas
You can use Grid panel. If you do not specify Row/Column of the controls inside the Grid, they will overlap.
Another solution is to use Canvas layout, which is almost like what you have on WinForms, where you can set X/Y of the control and you have more control on where they should appear.

C# Winforms Resizing and Controls

Say I have a Winform with 3 adjacent Textboxes, all within a Panel that covers the majority of the form. How can I configure it so that when I resize the form, the controls also resize, but do not bleed into each other? I've tried the different anchoring options, however the textboxes always tend to run into each other?
It's probably something really simple that i've missed.
Thanks.
One easy way to accomplish this is to put the three textboxes into a TableLayoutPanel that has 1 row and three columns.
Settings:
Set the width of each column to 33%
Set the anchor properties of the TableLayoutPanel to Top, Left, Right (or similar).
Set the Dock property of each TextBox to Fill.
Have a look at the TableLayoutPanel - you should be able to get the sort of behaviour you want using one of those with 3 columns and a text box in each one.

Categories