Skip to content

Commit

Permalink
Merge pull request #1888 from cwensley/curtis/gridcolum-alignment-wit…
Browse files Browse the repository at this point in the history
…h-style

Wpf: Inherit DataGrid.ColumnHeaderStyle when header text is aligned
  • Loading branch information
cwensley authored Feb 3, 2021
2 parents c8dea9a + 063aed0 commit 6b7cefe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
38 changes: 26 additions & 12 deletions src/Eto.Wpf/Forms/Controls/GridColumnHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using Eto.Wpf.Forms.Cells;
using swc = System.Windows.Controls;
using sw = System.Windows;
using swd = System.Windows.Data;
using Eto.Forms;
using System.Globalization;
using System;

namespace Eto.Wpf.Forms.Controls
{
public interface IGridHandler
{
EtoDataGrid Control { get; }
Grid Widget { get; }
bool Loaded { get; }
bool DisableAutoScrollToSelection { get; }
Expand All @@ -20,14 +24,16 @@ public interface IGridColumnHandler : GridColumn.IHandler
swc.DataGridColumn Control { get; }

void OnLoad();

void SetHeaderStyle();
}


public class GridColumnHandler : WidgetHandler<swc.DataGridColumn, GridColumn>, IGridColumnHandler, ICellContainerHandler
{
Cell dataCell;

public IGridHandler GridHandler { get; set; }
public IGridHandler GridHandler { get; private set; }

protected override void Initialize()
{
Expand Down Expand Up @@ -182,6 +188,7 @@ public bool Visible
public void Setup(IGridHandler gridHandler)
{
GridHandler = gridHandler;
SetHeaderStyle();
}

public sw.FrameworkElement SetupCell(ICellHandler handler, sw.FrameworkElement defaultContent, swc.DataGridCell cell)
Expand Down Expand Up @@ -225,17 +232,24 @@ public TextAlignment HeaderTextAlignment
set
{
if (Widget.Properties.TrySet(HeaderTextAlignment_Key, value))
{
if (value == TextAlignment.Left)
Control.HeaderStyle = null;
else
{
var style = new sw.Style();
style.TargetType = typeof(swc.Primitives.DataGridColumnHeader);
style.Setters.Add(new sw.Setter(swc.Primitives.DataGridColumnHeader.HorizontalContentAlignmentProperty, value.ToWpf()));
Control.HeaderStyle = style;
}
}
SetHeaderStyle();
}
}

public virtual void SetHeaderStyle()
{
var alignment = HeaderTextAlignment;
if (alignment == TextAlignment.Left)
{
Control.ClearValue(swc.DataGridColumn.HeaderStyleProperty);
}
else if (GridHandler != null)
{
var style = new sw.Style();
style.BasedOn = GridHandler.Control.ColumnHeaderStyle;
style.TargetType = typeof(swc.Primitives.DataGridColumnHeader);
style.Setters.Add(new sw.Setter(swc.Primitives.DataGridColumnHeader.HorizontalContentAlignmentProperty, alignment.ToWpf()));
Control.HeaderStyle = style;
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/Eto.Wpf/Forms/Controls/GridHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ protected override void Initialize()
HandleEvent(Eto.Forms.Control.MouseUpEvent);

Control.Loaded += Control_Loaded;

// Listen to changes of the column header style so we can apply column styles appropriately for alignment
Widget.Properties.Set(swc.DataGrid.ColumnHeaderStyleProperty, PropertyChangeNotifier.Register(swc.DataGrid.ColumnHeaderStyleProperty, Control_ColumnHeaderStyleChanged, Control));
}

private void Control_ColumnHeaderStyleChanged(object sender, EventArgs e)
{
foreach (var col in Widget.Columns)
{
if (col.Handler is IGridColumnHandler columnHandler)
{
columnHandler.SetHeaderStyle();
}
}
}

private void Control_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -301,14 +315,14 @@ protected class ColumnCollection : EnumerableChangedHandler<GridColumn, GridColu
public override void AddItem(GridColumn item)
{
var colhandler = (GridColumnHandler)item.Handler;
colhandler.GridHandler = Handler;
colhandler.Setup(Handler);
Handler.Control.Columns.Add(colhandler.Control);
}

public override void InsertItem(int index, GridColumn item)
{
var colhandler = (GridColumnHandler)item.Handler;
colhandler.GridHandler = Handler;
colhandler.Setup(Handler);
Handler.Control.Columns.Insert(index, colhandler.Control);
}

Expand Down

0 comments on commit 6b7cefe

Please sign in to comment.