示例#1
0
        private double GetComputedHeight(bool allowNaN)
        {
            var size = GridLengthUtil.ComputeValue(RenderSize.Height, HeaderHeight);

            if (double.IsNaN(size) && !allowNaN)
            {
                _headerControl.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                return(_headerControl.DesiredSize.Height);
            }
            return(size);
        }
示例#2
0
        private static void OnRowDefinitionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var row        = (string)e.NewValue;
            var element    = d as FrameworkElement;
            var parentGrid = element.Parent as Grid;

            if (parentGrid == null)
            {
                return;
            }

            Grid.SetRow(element, parentGrid.RowDefinitions.Count);

            parentGrid.RowDefinitions.Add(new RowDefinition()
            {
                Height = GridLengthUtil.ConvertToGridLength(row)
            });
        }
示例#3
0
        private static void OnColumnDefinitionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var column     = (string)e.NewValue;
            var element    = d as FrameworkElement;
            var parentGrid = element.Parent as Grid;

            if (parentGrid == null)
            {
                return;
            }

            Grid.SetColumn(element, parentGrid.ColumnDefinitions.Count);

            parentGrid.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = GridLengthUtil.ConvertToGridLength(column)
            });
        }
示例#4
0
        private static void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            var dataGrid = sender as DataGrid;

            #region Get User Custom DataGridColumnAttribute

            var visibility          = e.Column.Visibility;
            var width               = e.Column.Width;
            var header              = e.Column.Header;
            var readOnly            = e.Column.IsReadOnly;
            var bindingMode         = BindingMode.TwoWay;
            var updateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

            var descriptor = e.PropertyDescriptor as PropertyDescriptor;
            foreach (Attribute attribute in descriptor.Attributes)
            {
                if (attribute is IgnoreColumnAttribute)
                {
                    visibility = Visibility.Collapsed;
                }
                if (attribute is ColumnWidthAttribute)
                {
                    width = GridLengthUtil.ConvertToDataGridLength((attribute as ColumnWidthAttribute).Width);
                }
                if (attribute is DisplayNameAttribute)
                {
                    header = (attribute as DisplayNameAttribute).DisplayName;
                }
                if (attribute is ReadOnlyColumnAttribute)
                {
                    readOnly = true;
                }
                if (attribute is ColumnBindingAttribute)
                {
                    var binding = attribute as ColumnBindingAttribute;
                    bindingMode         = binding.BindingMode;
                    updateSourceTrigger = binding.UpdateSourceTrigger;
                }
            }
            #endregion

            if (e.PropertyType.IsEnum)
            {
                var newColumn = new DataGridComboBoxColumn()
                {
                    Width      = width,
                    Header     = header,
                    IsReadOnly = readOnly,
                    Visibility = visibility,
                };

                newColumn.ItemsSource         = Enum.GetValues(e.PropertyType).Cast <Enum>();
                newColumn.SelectedItemBinding = new Binding(e.PropertyName)
                {
                    Mode = bindingMode, UpdateSourceTrigger = updateSourceTrigger
                };
                newColumn.EditingElementStyle = new Style(typeof(ComboBox))
                {
                    BasedOn = (Style)dataGrid.FindResource(typeof(ComboBox))
                };
                newColumn.EditingElementStyle.Setters.Add(new Setter(ComboBox.HeightProperty, 30.0));
                newColumn.EditingElementStyle.Setters.Add(new Setter(ComboBox.PaddingProperty, new Thickness(5, 0, 20, 0)));

                e.Column = newColumn;
            }
            else if (e.PropertyType == typeof(bool))
            {
                var newColumn = new DataGridCheckBoxColumn()
                {
                    Width      = width,
                    Header     = header,
                    IsReadOnly = readOnly,
                    Visibility = visibility,
                };

                newColumn.Binding = new Binding(e.PropertyName)
                {
                    Mode = bindingMode, UpdateSourceTrigger = updateSourceTrigger
                };

                var userStyle = GetAutoGenerateCheckBoxStyle(dataGrid);
                if (userStyle == null)
                {
                    newColumn.ElementStyle = new Style(typeof(CheckBox))
                    {
                        BasedOn = (Style)dataGrid.FindResource(typeof(CheckBox))
                    };
                    newColumn.ElementStyle.Setters.Add(new Setter(CheckBox.BorderThicknessProperty, new Thickness(1)));
                    newColumn.ElementStyle.Setters.Add(new Setter(CheckBox.BackgroundProperty, Colors.Transparent.ToBrush()));
                    newColumn.ElementStyle.Setters.Add(new Setter(CheckBoxHelper.GlyphBrushProperty, Colors.Transparent.ToBrush()));


                    newColumn.EditingElementStyle = new Style(typeof(CheckBox))
                    {
                        BasedOn = (Style)dataGrid.FindResource(typeof(CheckBox))
                    };
                    newColumn.EditingElementStyle.Setters.Add(new Setter(CheckBox.BorderThicknessProperty, new Thickness(1)));
                    newColumn.EditingElementStyle.Setters.Add(new Setter(CheckBox.BackgroundProperty, Colors.Transparent.ToBrush()));
                    newColumn.EditingElementStyle.Setters.Add(new Setter(CheckBoxHelper.GlyphBrushProperty, Colors.Transparent.ToBrush()));
                }
                else
                {
                    newColumn.ElementStyle        = userStyle;
                    newColumn.EditingElementStyle = userStyle;
                }

                if (dataGrid.IsReadOnly)
                {
                    newColumn.ElementStyle.Setters.Add(new Setter(CheckBox.IsEnabledProperty, false));
                    newColumn.ElementStyle.Setters.Add(new Setter(CheckBox.OpacityProperty, 1.0));
                }

                newColumn.CellStyle = new Style(typeof(DataGridCell))
                {
                    BasedOn = (Style)dataGrid.FindResource(typeof(DataGridCell)),
                };

                e.Column = newColumn;
            }
            else
            {
                var newColumn = new DataGridTextColumn()
                {
                    Width      = width,
                    Header     = header,
                    IsReadOnly = readOnly,
                    Visibility = visibility,
                };

                newColumn.Binding = new Binding(e.PropertyName)
                {
                    Mode = bindingMode, UpdateSourceTrigger = updateSourceTrigger
                };

                newColumn.ElementStyle = new Style(typeof(TextBlock))
                {
                    BasedOn = (Style)dataGrid.FindResource(typeof(TextBlock))
                };

                newColumn.ElementStyle.Setters.Add(new Setter(TextBox.MaxHeightProperty, 100.0));

                newColumn.EditingElementStyle = new Style(typeof(TextBox))
                {
                    BasedOn = (Style)dataGrid.FindResource(typeof(TextBox))
                };

                newColumn.EditingElementStyle.Setters.Add(new Setter(TextBox.MaxHeightProperty, 100.0));
                newColumn.EditingElementStyle.Setters.Add(new Setter(TextBox.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Auto));
                newColumn.EditingElementStyle.Setters.Add(new Setter(TextBox.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Auto));
                newColumn.EditingElementStyle.Setters.Add(new Setter(TextBox.MarginProperty, new Thickness(2)));
                newColumn.EditingElementStyle.Setters.Add(new Setter(TextBoxHelper.FocusedShadowColorProperty, Colors.Transparent));

                newColumn.CellStyle = new Style(typeof(DataGridCell))
                {
                    BasedOn = (Style)dataGrid.FindResource(typeof(DataGridCell)),
                };
                newColumn.CellStyle.Setters.Add(new Setter(DataGridCell.PaddingProperty, new Thickness(0, 0, 10, 0)));

                e.Column = newColumn;
            }
        }
示例#5
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var length = value?.ToString();

            return(GridLengthUtil.ConvertToGridLength(length));
        }