Beispiel #1
0
        public void GetPropertiesTest()
        {
            IList <PropertyInfo> list = PropertyInfoEngine.GetProperties(typeof(MockObject));

            //this should be 1 since only one property is CanRead
            Assert.AreEqual(1, list.Count, "number of properties is incorrect");
            Assert.AreEqual("Test", list[0].Name, "invalid property created");

            IList <string> list2 = PropertyInfoEngine.GetProperties(typeof(MockObject), delegate(PropertyInfo prop)
            {
                return(prop.Name);
            });

            Assert.AreEqual(1, list.Count, "invalid number of properties");
            Assert.AreEqual("Test", list2[0], "invalid data loaded");
        }
Beispiel #2
0
        //generates the column for the view
        private void GenerateGridColumns(Type type)
        {
            //unregister to the old columns notifications
            if (!UseDefaultView && View != null)
            {
                GridView oldView = View as GridView;
                if (oldView != null)
                {
                    foreach (INotifyPropertyChanged column in oldView.Columns)
                    {
                        column.PropertyChanged -= gridViewColumnPropertyChanged;
                    }
                }
            }
            GridView gridView = new GridView();//grid view to display the columns

            IList <DataGridViewColumn> columnsToAdd = PropertyInfoEngine.GetProperties <DataGridViewColumn>(
                type, delegate(PropertyInfo property)
            {
                DataGridViewColumn column = null;
                //get the attribute
                DataGridViewPropertyDescriptorAttribute[] attributes = (DataGridViewPropertyDescriptorAttribute[])
                                                                       property.GetCustomAttributes(typeof(DataGridViewPropertyDescriptorAttribute), true);
                if (attributes != null && attributes.Length > 0)
                {
                    if (attributes[0].Exclude)
                    {
                        return(null);    //return null so that we exclude this column
                    }
                    column = attributes[0].GetDataGridViewColumn();
                }
                else
                {
                    column = new DataGridViewColumn(property.Name, property.Name);
                    column.DisplayMemberBinding = new Binding(property.Name);
                }

                //register to the notifications
                ((INotifyPropertyChanged)column).PropertyChanged += gridViewColumnPropertyChanged;
                return(column);
            });

            //add all the columns to the new grid view, and remove the excluded
            for (int i = 0; i < columnsToAdd.Count; i++)
            {
                DataGridViewColumn column = columnsToAdd[i];
                if (column != null)
                {
                    gridView.Columns.Add(column);
                }
                else
                {
                    columnsToAdd.RemoveAt(i);
                    i--;
                }
            }

            //position the columns properly
            for (int i = 0; i < columnsToAdd.Count; i++)
            {
                DataGridViewColumn columnToMove = columnsToAdd[i];
                if (DataGridViewColumn.IsValidPosition(columnToMove.DefaultPosition))
                {
                    gridView.Columns.Move(i, (columnToMove.DefaultPosition >= columnsToAdd.Count ? (columnsToAdd.Count - 1) : columnToMove.DefaultPosition));
                }
            }

            View = gridView;
        }