WPF binding with List of object but index as row and columns

jacques labuschagne 1 Reputation point
2020-07-23T07:08:26.813+00:00

Hallo

Ok we are new to WPF and realised the old DataTable with link as datasource to a datagridView is not possible.

So populated our data in a List of Objects but the objects can be variable based on data we recieve from SQL, the dataGrid needs to be manipulated as a tableMatrix with Rows and Column based on some properties in each of the objects. It also holds a text field for a value to be displayed in the "cell" and also have access to a background colour in one of the properties. (One object example below);

13443-exposedobject.jpg

We tried to follow the MVVM pattern, some items might still be in the wrong classes, but learn as we go along. Below is the ViewModel that will create a list of objects based on the model classes.

The question is how to bind this to the XAML so we can have a layout of the objects as per picture below, keep in mind we don't have a fixed Column headers.

13405-activewallrequired.jpg

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,706 questions
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-07-24T03:32:14.177+00:00

    I will give you a sample of changing the selected cell's background to Red for you to refer:

     private void dgSourceData_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)  
            {  
                int colindex = -1;  
                int rowindex = -1;  
                colindex = e.Column.DisplayIndex;//Get the selected Column Number  
                rowindex = e.Row.GetIndex();//Get the selected cell Row number  
                DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowindex);//Get the Row   
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);//Get all cells   
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(colindex);//lock the selected cell  
                if (cell != null)  
                {  
                    dataGrid.ScrollIntoView(row, dataGrid.Columns[colindex]);  
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(colindex);  
                    cell.Focus();  
                    cell.Background = new SolidColorBrush(Colors.Red);//change the properties of the cell  
                }  
      
            }  
      
            public static T GetVisualChild<T>(Visual parent) where T : Visual  
            {  
                T childContent = default(T);  
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);  
                for (int i = 0; i < numVisuals; i++)  
                {  
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);  
                    childContent = v as T;  
                    if (childContent == null)  
                    {  
                        childContent = GetVisualChild<T>(v);  
                    }  
                    if (childContent != null)  
                    { break; }  
                }  
                return childContent;  
            }  
    

    After edit the cell, you can see as below:

    13553-3.gif

    0 comments No comments

  2. Peter Fleischer (former MVP) 19,306 Reputation points
    2020-07-24T09:29:37.973+00:00

    Hi Jacques,
    to have control of each of the cells you can use Style and get UI like this:

    13547-x.png

    Forum software deny to show code and deny include Link to my OneDrive.

    I uploaded demo project to GitHub: https://github.com/fips1950/VariableColumnDataGrid_jacqueslabuschagne-5550

    0 comments No comments