How to dynamically populate a WPF grid using data binding? - c#

I am looking for a way to populate a single grid using data bindings.
The way I do this at the moment is by using an ItemsControl where each item is represented as a Grid with the columns that I need. The main reason I want a single grid is to make the widths of all columns line up.
Is there way for the Grid panel to be used with ItemsControl so that there is a single grid to contain all the items?
Or is there another solution?

Why not just use DataGrid?

You can make grid column widths "line up" across grids by using Grid.IsSharedSizeScope and SharedSizeGroup.
You simply need to set the Grid.IsSharedSizeScope property to true on the element that contains your grids, then set the SharedSizeGroup on the ColumnDefinitions you want to have the same width.
Both of the links above have examples.

Use a ListView with a GridView embedded in the ListView.View property. There are examples of this here and here.

Related

different controls in one column gridview, devexpress winform

How add different controls in one column GridView?
I want it to be like this.
Without creating separate gridviews, for other controls.
As far as I see, each row has its own type and the same editor should edit cells in this row. For this scenario, I think it's better to use the Vertical Grid instead of the Grid View.
If you still wish to use the Grid View, use the GridView.CustomRowCellEdit and GridView.CustomRowCellEditForEditing events to assign the required editor to a grid cell.

WPF control similar to ObjectListView

I'm writing an application using WPF, and I need to make something that looks like this (see image):
What control should I use for this?
If you don’t need the groupings (the “Hire as chef” and “Seek dinner invitation” headings), then DataGrid should get you close. You can bind its ItemsSource to your collection of items, and define custom columns bound to your items’ properties.
For example, you can use a DataGridTextColumn for “Occupation”, DataGridCheckBoxColumn for “Tells Jokes?”, and DataGridTemplateColumn for more complex properties which require a custom DataTemplate to visualize, such as the main “Person” column or “Cooking skill”.
Edi: It appears that the DataGrid does support grouping as well.
You should go for ListView
http://msdn.microsoft.com/en-us/library/ms754027(v=vs.90).aspx

Accessing a cell in the GridView of a ListView

If I have a grid view within a listview and this grid view has definitions for 3 columns.
for each cell in the grid is a textbox, if I want to access a specific "cell" in the gridview, how do I go about doing it? I am accessing it from the C# side.
Don't try to manipulate the cells directly; although it is possible, it's difficult and counter-intuitive. You should manipulate the bound objects instead.

Creating a dynamic grid control

While working in WPF i have the need for a Dynamic Grid. By this i mean a grid that contains only one kind of object, has a template for that object etc. But unlike a similar ItemsControl like a Listbox, i want the grid to be given a Maximum Columns property. This should act as a delimiter which will then calculate the number of rows needed based on the number of objects within the grid. To do this, i thought of inherriting a Grid to make use of its Row and Column properties, but i have a problem... I dont know how to implement an ItemsSource property outside of inherriting the ItemsSource from an ItemsControl...
so my question comes in two parts...
Am i pursuing this the right way? should i be inherriting ItemsControl and trying to re-implement the Grid behavior
if this is the right way to do it, how do i implement an ItemsSource property with its corresponding ItemTemplate
Perhaps a better way would be to use a ListView? Here is an example how to achieve 3-column output: http://kristofmattei.be/2010/03/16/multi-column-listview/
Do you want something like UniformGrid? If you set the Columns property (and don't set the Rows property), it will automatically figure out how many rows to create to hold its items.

Prevent resize of content in WPF

I have created a data template for a data class. The width of the grid created by the datatemplate is bound to a property in the data class, ie, the size of the control matters.
I am creating a list of the data class objects, and adding them to an items control in MainWindow.
Update:
I would like to line up the controls from the data list one after another, and not have them resize.
Example:
[---------box1----------][--box2--][------box3-----]
each are the same data template type, and different widths. Perhaps I shouldn't be using itemsControl, but I am unsure of what other control to use to achieve this.
Any help would be much appreciated. I can post XAML
Can you post some XAML.
It can depend on the HorizontalContentAlignment of the ItemsControl or the style of your ItemContainer/ItemPanel.

Categories