Beispiel #1
0
        void CreateCellContent(DataGrid grid, DataGridPanel panel, Border bdr, CellRange rng)
        {
            // get row and column
            var r = panel.Rows[rng.Row];
            var c = panel.Columns[rng.Column];

            //var gr = r as GroupRow;

            // honor user templates (if the row has a backing data item)
            if (r.DataItem != null && c.CellTemplate != null && panel.CellType == CellType.Cell)
            {
                bdr.Padding = GetTemplatePadding(bdr.Padding);
                bdr.Child   = GetTemplatedCell(grid, c.CellTemplate);
                return;
            }

            // get binding, unbound value for the cell
            var b     = r.DataItem != null ? c.Binding : null;
            var ubVal = r.DataItem != null ? null : panel[rng.Row, rng.Column];

            // get foreground brush taking selected state into account
            var fore = GetForegroundBrush(grid, r, rng, grid.Foreground);

            // handle non-text types
            var       type = c.DataType;
            TextBlock tb   = null;
            CheckBox  chk  = null;

            if (                                                 // not a group, or hierarchical
                panel.CellType == CellType.Cell &&
                (type == typeof(bool) || type == typeof(bool?))) // and bool
            {
                // Boolean cells: use CheckBox
                chk = new CheckBox();
                chk.IsThreeState        = type == typeof(bool?);
                chk.HorizontalAlignment = HorizontalAlignment.Center;
                chk.VerticalAlignment   = VerticalAlignment.Center;
                chk.MinWidth            = 32;

                chk.HorizontalAlignment = HorizontalAlignment.Stretch;
                chk.VerticalAlignment   = VerticalAlignment.Stretch;
                chk.Margin = new Thickness(0, -10, 0, -10);

                chk.IsHitTestVisible = false;
                chk.IsTabStop        = false;
                if (fore != null)
                {
                    chk.Foreground = fore;
                }

                bdr.Child = chk;
                if (b != null)
                {
                    chk.SetBinding(CheckBox.IsCheckedProperty, b);
                }
                else
                {
                    var value = ubVal as bool?;
                    chk.IsChecked = value;
                }
            }
            else
            {
                // use TextBlock for everything else (even empty cells)
                tb = new TextBlock();
                tb.VerticalAlignment = VerticalAlignment.Center;
                if (fore != null)
                {
                    tb.Foreground = fore;
                }
                bdr.Child = tb;

                // bound values
                if (b != null)
                {
                    // apply binding
                    tb.SetBinding(TextBlock.TextProperty, b);
                }
                else if (ubVal != null) // unbound values
                {
                    // get column format from column and/or binding
                    tb.Text = r.GetDataFormatted(c);
                }
            }
        }
Beispiel #2
0
 public virtual bool IsSortSymbolRow(DataGrid grid, CellRange rng)
 {
     return(rng.BottomRow == grid.ColumnHeaders.Rows.Count - 1);
 }
Beispiel #3
0
 public virtual void CreateCellContent(DataGrid grid, Border bdr, CellRange rng)
 {
     CreateCellContent(grid, grid.Cells, bdr, rng);
 }
Beispiel #4
0
        public virtual void CreateColumnHeaderContent(DataGrid grid, Border bdr, CellRange rng)
        {
            var c = grid.Columns[rng.Column];

            if (c.HeaderObject != null)
            {
                var tb = new ContentPresenter();
                tb.VerticalContentAlignment = VerticalAlignment.Center;
                tb.Content = c.HeaderObject;
                bdr.Child  = tb;
                // apply global foreground color
                if (grid.ColumnHeaderForeground != null)
                {
                    tb.Foreground = grid.ColumnHeaderForeground;
                }
                if (c.HeaderForeground != null)
                {
                    tb.Foreground = c.HeaderForeground;
                }
                tb.HorizontalAlignment = c.HeaderHorizontalAlignment;
                // show sort direction (after applying styles)
                if (grid.ShowSort && IsSortSymbolRow(grid, rng))
                {
                    // get the grid that owns the row (we could be printing this)
                    var g = grid.Columns.Count > 0 ? grid.Columns[0].Grid : grid;

                    // get sort direction and show it
                    ListSortDirection?sd = g.GetColumnSort(rng.Column);
                    if (sd.HasValue)
                    {
                        var icon = GetGlyphSort(sd.Value, tb.Foreground);
                        SetBorderContent(bdr, bdr.Child, icon, 1);
                    }
                }
            }
            else
            {
                // create TextBlock content
                var tb = new TextBlock();
                tb.VerticalAlignment = VerticalAlignment.Center;
                bdr.Child            = tb;

                // get unbound value
                var value = grid.ColumnHeaders[rng.Row, rng.Column];
                if (value != null)
                {
                    tb.Text = value.ToString();
                }

                // no unbound value? show column caption on first fixed row
                if (string.IsNullOrEmpty(tb.Text) && rng.Row == 0)
                {
                    tb.Text = grid.Columns[rng.Column].GetHeader() ?? "";
                }

                // apply global foreground color
                if (grid.ColumnHeaderForeground != null)
                {
                    tb.Foreground = grid.ColumnHeaderForeground;
                }

                // honor HeaderTemplate property

                if (c.HeaderTemplate != null)
                {
                    bdr.Padding = GetTemplatePadding(bdr.Padding); // Util.ThicknessEmpty;
                    bdr.Child   = GetTemplatedCell(grid, c.HeaderTemplate);
                    //bdr.Child = c.HeaderTemplate.LoadContent() as UIElement;
                }

                if (c.HeaderForeground != null)
                {
                    tb.Foreground = c.HeaderForeground;
                }
                tb.HorizontalAlignment = c.HeaderHorizontalAlignment;

                // show sort direction (after applying styles)
                if (grid.ShowSort && IsSortSymbolRow(grid, rng))
                {
                    // get the grid that owns the row (we could be printing this)
                    var g = grid.Columns.Count > 0 ? grid.Columns[0].Grid : grid;

                    // get sort direction and show it
                    ListSortDirection?sd = g.GetColumnSort(rng.Column);
                    if (sd.HasValue)
                    {
                        var icon = GetGlyphSort(sd.Value, tb.Foreground);
                        SetBorderContent(bdr, bdr.Child, icon, 1);
                    }
                }
            }
        }
Beispiel #5
0
        public virtual Thickness GetCellPadding(DataGrid grid, DataGridPanel panel, CellRange rng)
        {
            var p = _padding;

            return(p);
        }