Пример #1
0
        /// <summary>
        /// Устанавливает заголовки
        /// </summary>
        private void SetHeaders()
        {
            listViewCompliance.Columns.Clear();

            try
            {
                List <PropertyInfo> properties = GetTypeProperties();
                ColumnHeader        columnHeader;
                foreach (PropertyInfo propertyInfo in properties)
                {
                    ListViewDataAttribute attr =
                        (ListViewDataAttribute)propertyInfo.GetCustomAttributes(typeof(ListViewDataAttribute), false)[0];
                    columnHeader       = new ColumnHeader();
                    columnHeader.Width = (int)(listViewCompliance.Width * attr.HeaderWidth);
                    columnHeader.Text  = attr.Title;
                    columnHeader.Tag   = propertyInfo.PropertyType;
                    listViewCompliance.Columns.Add(columnHeader);
                }
            }
            catch (Exception ex)
            {
                Program.Provider.Logger.Log("Error while building list headers", ex);
                return;
            }
        }
        ///<summary>
        ///Raises the <see cref="E:System.Windows.Forms.Control.SizeChanged"></see> event.
        ///</summary>
        ///
        ///<param name="e">An <see cref="T:System.EventArgs"></see> that contains the event data. </param>
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);

            if (_viewedType == null)
            {
                return;
            }

            //определение своиств типа
            List <PropertyInfo> preProrerty = GetTypeProperties();
            //определение своиств, имеющих атрибут "отображаемое в списке"
            List <PropertyInfo> properties =
                preProrerty.Where(p => p.GetCustomAttributes(typeof(ListViewDataAttribute), false).Length != 0).ToList();

            for (int i = 0; i < ColumnHeaderList.Count; i++)
            {
                if (i >= properties.Count)
                {
                    ColumnHeaderList[i].Width = (int)(itemsListView.Width * 0.1f);
                    continue;
                }
                ListViewDataAttribute attr =
                    (ListViewDataAttribute)properties[i].GetCustomAttributes(typeof(ListViewDataAttribute), false)[0];
                ColumnHeaderList[i].Width = attr.HeaderWidth > 1
                                         ? (int)attr.HeaderWidth
                                         : (int)(itemsListView.Width * attr.HeaderWidth);
            }
        }
Пример #3
0
        /// <summary>
        /// Устанавливает заголовки
        /// </summary>
        protected virtual void SetHeaders()
        {
            ColumnHeaderList.Clear();

            try
            {
                List <PropertyInfo> properties = GetTypeProperties();
                ColumnHeader        columnHeader;
                foreach (PropertyInfo propertyInfo in properties)
                {
                    ListViewDataAttribute attr =
                        (ListViewDataAttribute)propertyInfo.GetCustomAttributes(typeof(ListViewDataAttribute), false)[0];
                    columnHeader       = new ColumnHeader();
                    columnHeader.Width = attr.HeaderWidth > 1 ? (int)attr.HeaderWidth : (int)(itemsListView.Width * attr.HeaderWidth);
                    columnHeader.Text  = attr.Title;
                    columnHeader.Tag   = propertyInfo.PropertyType;
                    ColumnHeaderList.Add(columnHeader);
                }
                itemsListView.Columns.AddRange(ColumnHeaderList.ToArray());
            }
            catch (Exception ex)
            {
                Program.Provider.Logger.Log("Error while building list headers", ex);
                return;
            }
        }
        protected virtual List <PropertyInfo> GetTypeProperties()
        {
            if (_viewedType == null)
            {
                throw new NullReferenceException("_viewedType is null");
            }
            //определение своиств, имеющих атрибут "отображаемое в списке"
            List <PropertyInfo> properties =
                _viewedType.GetProperties().Where(p => p.GetCustomAttributes(typeof(ListViewDataAttribute), false).Length != 0).ToList();

            //поиск своиств у которых задан порядок отображения
            //своиства, имеющие порядок отображения
            Dictionary <int, PropertyInfo> orderedProperties = new Dictionary <int, PropertyInfo>();
            //своиства, НЕ имеющие порядок отображения
            List <PropertyInfo> unOrderedProperties = new List <PropertyInfo>();

            foreach (PropertyInfo propertyInfo in properties)
            {
                ListViewDataAttribute lvda = (ListViewDataAttribute)
                                             propertyInfo.GetCustomAttributes(typeof(ListViewDataAttribute), false).FirstOrDefault();
                if (lvda.Order > 0)
                {
                    orderedProperties.Add(lvda.Order, propertyInfo);
                }
                else
                {
                    unOrderedProperties.Add(propertyInfo);
                }
            }

            var ordered = orderedProperties.OrderBy(p => p.Key).ToList();

            properties.Clear();
            properties.AddRange(ordered.Select(keyValuePair => keyValuePair.Value));
            properties.AddRange(unOrderedProperties);

            return(properties);
        }
        /// <summary>
        /// Устанавливает заголовки
        /// </summary>
        protected virtual void SetHeaders()
        {
            if (!(itemsListView.View is GridView))
            {
                itemsListView.View = new GridView();
            }

            ColumnHeaderList.Clear();

            try
            {
                List <PropertyInfo> properties = GetTypeProperties();
                GridViewColumn      columnHeader;
                foreach (PropertyInfo propertyInfo in properties)
                {
                    ListViewDataAttribute attr =
                        (ListViewDataAttribute)propertyInfo.GetCustomAttributes(typeof(ListViewDataAttribute), false)[0];

                    #region Определение ЭУ

                    if (propertyInfo.PropertyType.IsSubclassOf(typeof(StaticDictionary)))
                    {
                        //object val = propertyInfo.GetValue(obj, null);

                        //if (propertyInfo.PropertyType.GetCustomAttributes(typeof(TableAttribute), false).Length > 0)
                        //{
                        //    DictionaryComboBox dc = new DictionaryComboBox
                        //    {
                        //        Enabled = controlEnabled,
                        //        Name = propertyInfo.Name,
                        //        SelectedItem = (StaticDictionary)val,
                        //        Tag = propertyInfo,
                        //        Type = propertyInfo.PropertyType,
                        //    };
                        //    //для возможности вызова новой вкладки
                        //    Program.MainDispatcher.ProcessControl(dc);
                        //    //
                        //    return dc;
                        //}
                        Type         t = propertyInfo.PropertyType;
                        PropertyInfo p = t.GetProperty("Items");

                        ConstructorInfo  ci       = t.GetConstructor(new Type[0]);
                        StaticDictionary instance = (StaticDictionary)ci.Invoke(null);

                        IEnumerable staticList = (IEnumerable)p.GetValue(instance, null);

                        DataTemplate            template = new DataTemplate();
                        FrameworkElementFactory factory  = new FrameworkElementFactory(typeof(System.Windows.Controls.ComboBox));
                        template.VisualTree = factory;

                        //System.Windows.Data.Binding sourceBinding = new System.Windows.Data.Binding();
                        //sourceBinding.Source = staticList;
                        //BindingOperations.SetBinding(taskItemListView, ListView.ItemsSourceProperty, sourceBinding );

                        factory.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding {
                            Source = staticList
                        });

                        //childFactory = new FrameworkElementFactory(typeof(Label));

                        //childFactory.SetBinding(Label.ContentProperty, new Binding("Machine.Descriiption"));

                        //childFactory.SetValue(Label.WidthProperty, 170.0);

                        //childFactory.SetValue(Label.HorizontalAlignmentProperty, HorizontalAlignment.Center);

                        //factory.AppendChild(childFactory);
                        columnHeader = new GridViewColumn {
                            CellTemplate = template
                        };
                    }
                    else if (propertyInfo.PropertyType.IsSubclassOf(typeof(AbstractDictionary)))
                    {
                        IDictionaryCollection dc;
                        try
                        {
                            dc = GlobalObjects.CasEnvironment.Dictionaries[propertyInfo.PropertyType];
                        }
                        catch (Exception)
                        {
                            dc = null;
                        }

                        DataTemplate            template = new DataTemplate();
                        FrameworkElementFactory factory  = new FrameworkElementFactory(typeof(System.Windows.Controls.ComboBox));
                        template.VisualTree = factory;

                        //System.Windows.Data.Binding sourceBinding = new System.Windows.Data.Binding();
                        //sourceBinding.Source = staticList;
                        //BindingOperations.SetBinding(taskItemListView, ListView.ItemsSourceProperty, sourceBinding );

                        if (dc != null)
                        {
                            List <KeyValuePair <string, IDictionaryItem> > list = new List <KeyValuePair <string, IDictionaryItem> >();
                            foreach (IDictionaryItem item in dc)
                            {
                                list.Add(new KeyValuePair <string, IDictionaryItem>(item.ToString(), item));
                            }

                            factory.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding {
                                Source = list
                            });
                            factory.SetBinding(ItemsControl.DisplayMemberPathProperty, new System.Windows.Data.Binding("Key"));
                            factory.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding("Value"));
                        }

                        columnHeader = new GridViewColumn {
                            CellTemplate = template
                        };

                        //AbstractDictionary val = (AbstractDictionary)propertyInfo.GetValue(obj, null);
                        //DictionaryComboBox dc = new DictionaryComboBox
                        //{
                        //    Enabled = controlEnabled,
                        //    SelectedItem = val,
                        //    Tag = propertyInfo,
                        //    Type = propertyInfo.PropertyType,
                        //};
                        //для возможности вызова новой вкладки
                        //Program.MainDispatcher.ProcessControl(dc);
                        //
                        //return dc;
                    }
                    else if (propertyInfo.PropertyType.IsSubclassOf(typeof(BaseEntityObject)))
                    {
                        DataTemplate template = new DataTemplate();

                        FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));
                        factory.SetValue(System.Windows.Controls.TextBox.TextWrappingProperty, TextWrapping.Wrap);
                        factory.SetValue(TextBoxBase.IsReadOnlyProperty, true);
                        factory.SetBinding(System.Windows.Controls.TextBox.TextProperty, new System.Windows.Data.Binding {
                            Source = list
                        });
                        template.VisualTree = factory;

                        columnHeader = new GridViewColumn {
                            CellTemplate = template
                        };
                    }
                    else if (propertyInfo.PropertyType.IsEnum)
                    {
                        DataTemplate            template = new DataTemplate();
                        FrameworkElementFactory factory  = new FrameworkElementFactory(typeof(System.Windows.Controls.ComboBox));
                        template.VisualTree = factory;

                        List <KeyValuePair <string, object> > list = new List <KeyValuePair <string, object> >();
                        foreach (object o in Enum.GetValues(propertyInfo.PropertyType))
                        {
                            string    name = Enum.GetName(propertyInfo.PropertyType, o);
                            string    desc = name;
                            FieldInfo fi   = propertyInfo.PropertyType.GetField(name);
                            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                            if (attributes.Length > 0)
                            {
                                string s = attributes[0].Description;
                                if (!string.IsNullOrEmpty(s))
                                {
                                    desc = s;
                                }
                            }
                            list.Add(new KeyValuePair <string, object>(desc, o));
                        }

                        factory.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding {
                            Source = list
                        });
                        factory.SetBinding(ItemsControl.DisplayMemberPathProperty, new System.Windows.Data.Binding("Key"));
                        factory.SetBinding(ItemsControl.ItemsSourceProperty, new System.Windows.Data.Binding("Value"));

                        columnHeader = new GridViewColumn {
                            CellTemplate = template
                        };
                    }
                    else
                    {
                        #region  ЭУ для базовых типов

                        string typeName = propertyInfo.PropertyType.Name.ToLower();
                        switch (typeName)
                        {
                        case "string":
                        {
                            DataTemplate template = new DataTemplate();

                            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));
                            factory.SetValue(System.Windows.Controls.TextBox.TextWrappingProperty, TextWrapping.Wrap);
                            factory.SetValue(TextBoxBase.IsReadOnlyProperty, true);
                            template.VisualTree = factory;

                            columnHeader = new GridViewColumn {
                                CellTemplate = template
                            };

                            break;
                        }

                        case "int32":
                        case "int16":
                        {
                            DataTemplate template = new DataTemplate();

                            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));
                            factory.SetValue(System.Windows.Controls.TextBox.TextWrappingProperty, TextWrapping.Wrap);
                            factory.SetValue(TextBoxBase.IsReadOnlyProperty, true);
                            template.VisualTree = factory;

                            columnHeader = new GridViewColumn {
                                CellTemplate = template
                            };

                            break;
                        }

                        case "datetime":
                        {
                            DataTemplate template = new DataTemplate();

                            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));
                            factory.SetValue(System.Windows.Controls.TextBox.TextWrappingProperty, TextWrapping.Wrap);
                            factory.SetValue(TextBoxBase.IsReadOnlyProperty, true);
                            template.VisualTree = factory;

                            columnHeader = new GridViewColumn {
                                CellTemplate = template
                            };
                            break;
                        }

                        case "bool":
                        case "boolean":
                        {
                            DataTemplate template = new DataTemplate();

                            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.CheckBox));
                            template.VisualTree = factory;

                            columnHeader = new GridViewColumn {
                                CellTemplate = template
                            };

                            break;
                        }

                        case "double":
                        {
                            DataTemplate template = new DataTemplate();

                            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(System.Windows.Controls.TextBox));
                            factory.SetValue(System.Windows.Controls.TextBox.TextWrappingProperty, TextWrapping.Wrap);
                            factory.SetValue(TextBoxBase.IsReadOnlyProperty, true);
                            template.VisualTree = factory;

                            columnHeader = new GridViewColumn {
                                CellTemplate = template
                            };

                            break;
                        }

                        //case "directivethreshold":
                        //    {
                        //        object val = propertyInfo.GetValue(obj, null);
                        //        return new DirectiveThresholdControl { Enabled = controlEnabled, Threshold = (DirectiveThreshold)val, Tag = propertyInfo };
                        //    }
                        //case "detaildirectivethreshold":
                        //    {
                        //        object val = propertyInfo.GetValue(obj, null);
                        //        return new DetailDirectiveThresholdControl { Enabled = controlEnabled, Threshold = (DetailDirectiveThreshold)val, Tag = propertyInfo };
                        //    }
                        //case "lifelength":
                        //    {
                        //        object val = propertyInfo.GetValue(obj, null);
                        //        return new LifelengthViewer
                        //        {
                        //            Enabled = controlEnabled,
                        //            Lifelength = (Lifelength)val,
                        //            MinimumSize = new Size(20, 17),
                        //            Tag = propertyInfo
                        //        };
                        //    }
                        default:
                            columnHeader = null;
                            break;
                        }
                        #endregion
                    }
                    #endregion

                    if (columnHeader == null)
                    {
                        continue;
                    }

                    columnHeader.Width  = (int)(attr.HeaderWidth < 1 ? itemsListView.Width * attr.HeaderWidth : attr.HeaderWidth);
                    columnHeader.Header = attr.Title;
                    //columnHeader.Tag = propertyInfo;
                    //Поиск NotNullAttribute для определения возможности задавать пустые значения в колонке
                    //NotNullAttribute notNullAttribute =
                    //    (NotNullAttribute)propertyInfo.GetCustomAttributes(typeof(NotNullAttribute), false).FirstOrDefault();
                    //if (notNullAttribute != null)
                    //{
                    //    //Если имеется атрибут NotNullAttribute то шрифт заголовка задается Жирным
                    //    columnHeader.HeaderCell.Style.Font = new Font(dataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold);
                    //    columnHeader.HeaderCell.ToolTipText =
                    //        string.Format("The cells in column {0} should be filled", columnHeader.HeaderText);
                    //}
                    ColumnHeaderList.Add(columnHeader);
                }
            }
            catch (Exception ex)
            {
                Program.Provider.Logger.Log("Error while building list headers", ex);
                return;
            }
        }