locked
List or collection of object with MVVM pattern RRS feed

  • Question

  • Hallo

    I'm trying to create a model using MVVM priciples, but I'm a bit lost with the objects and list  of how to get this back so it can be displayed on a DataGrid. (This follow on the previous post but the question is different and more towards List and List of Objects and the pattern.

    So we created two classes for the Models. (After some time I realised this might be a option, due to the fact that we don't know how many rows and columns of data there will be at the end it might be worth breaking the object down to cells (Which do have the properties for row and Column), and then store multiple in a list and find a way to "glue" it all together on the WPF Window by making use of some Row and Column specification.

    Our Data is populated in SQL database as per attached:

    Each of the rows represents a pigeonlocation. 

    PigeonLocation will require the below data in each of the objects. (Could be any number of row any number of Columns based on the data from the SQL table)

    The properties is as per below and should create a single cell object.

     public class PigeonCellStatusModel
        {
            private string cellBackcolour;
            private string pigeonLocation;
            private bool allocatedToProduction;
            private string orderNr;
            private int aeraNr;
            private int zoneNr;
            private int rowNr;
            private int columnNr;        
            private bool pigeonEnabled;
    
            /// <summary>
            /// Pass row Data from SQL Datatable
            /// </summary>
            /// <param name="data"></param>
            public PigeonCellStatusModel(DataRow RowData)
            {
                var pigeonLocationData = new PigeonLocationModel(RowData[3].ToString());
    
                aeraNr = pigeonLocationData.AreaNr;
                zoneNr = pigeonLocationData.ZoneNr;
                rowNr = pigeonLocationData.RowNr;
                columnNr = pigeonLocationData.ColumnNr;
                pigeonEnabled = Convert.ToBoolean(RowData[5]);
                allocatedToProduction = Convert.ToBoolean(RowData[6]);
                orderNr = RowData[7].ToString();
            }
            public string CellBackColour
            {
                get { return cellBackcolour; }
                set
                {
                    if (orderNr.Length > 0)
                    {
                        cellBackcolour = "Green";
                    }
                    else if (!pigeonEnabled)
                    {
                        cellBackcolour = "Red";
                    }
                    else
                    {
                        cellBackcolour = "Grey";
                    }
                }
            }
            public bool AllocatedToProduction
            {
                get { return allocatedToProduction; }
            }
            
            public string OrderNr
            {
                get { return orderNr; }
            }       
        }

    The PigeonLocation column in SQL determine the Area | Zone | Row | Column (01020708 for instance is area 01, zone 02, Row 07, Column 08)

        public class PigeonLocationModel
        {
            private string pigeonLocation;
            private int aeraNr;
            private int zoneNr;
            private int rowNr;
            private int columnNr;        
           
            public PigeonLocationModel(string PigeonLocation)
            {
                pigeonLocation = PigeonLocation;
            }
            public int AreaNr
            {
                get { return aeraNr; }
                set
                {
                    aeraNr = Convert.ToInt32(pigeonLocation.Substring(0, 2));
                }
            }
            public int ZoneNr
            {
                get { return zoneNr; }
                set
                {
                    zoneNr = Convert.ToInt32(pigeonLocation.Substring(2, 2));
                }
            }
            public int RowNr
            {
                get { return rowNr; }
                set
                {
                    rowNr = Convert.ToInt32(pigeonLocation.Substring(4, 2));
                }
            }
    
            public int ColumnNr
            {
                get { return columnNr; }
                set
                {
                    columnNr = Convert.ToInt32(pigeonLocation.Substring(6, 2));
                }
            }
    
    
    
    
        }
    }

    The next step is where I'm stuck, not sure how to create a List of the above CellObjects, and if ICollection or another function is better to achieve this?

     public class PigeonDataSetModelView
        {
            private DataTable wallFromSql;
            public PigeonDataSetModelView(DataTable PigeonWallRecords)
            {
                wallFromSql = PigeonWallRecords;
            }
    
            public void GetDataSetPigeonWall()
            {
                var sqlTable = new DataTable();            
    
                var sqlDataTable = new tb_ProdPigeonStatusSql();
    
                sqlTable = sqlDataTable.SelectAllRecords();
    
    
                List<PigeonCellStatusModel> wallComplete = new List<PigeonCellStatusModel>();
    
                foreach (DataRow dr in sqlTable.Rows)
                {
                   
                }
                
            }
            
        }

    Also is this the best way to pass the data to the WPF Window with bindings in the behind code and not in XAML, need to run a foreach loop to populate the row and columns with the above properties for each specific Column and Row based on the property values.


    labjac

    Thursday, July 23, 2020 4:39 AM

All replies

  • Please post questions related to WPF in their forums at Microsoft Q&A. They can help you with MVVM stuff.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 23, 2020 1:58 PM