DataGridExVerticalLineAdorner - make some methods virtual
This commit is contained in:
1 parent
487c171813
commit
5fdc71ef29
1 file changed
+39
-48
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
@@ -13,22 +12,24 @@ namespace mbts.ui.controls
|
||||
{
|
||||
public class DataGridExVerticalLineAdorner : Adorner, IDisposable
|
||||
{
|
||||
public DataGridExVerticalLineAdorner(DataGridEx grid)
|
||||
: base(grid) {
|
||||
public DataGridExVerticalLineAdorner(DataGridEx grid) : base(grid) {
|
||||
_grid = grid;
|
||||
if (_grid.IsLoaded) _init();
|
||||
else _grid.Loaded += (s, e) => _init();
|
||||
this.attach();
|
||||
_grid.Loaded += _grid_Loaded;
|
||||
_grid.Unloaded += _grid_Unloaded;
|
||||
}
|
||||
|
||||
private readonly DataGridEx _grid;
|
||||
protected readonly DataGridEx _grid;
|
||||
private IList<IDisposable> _grid_hooks = new List<IDisposable>();
|
||||
private IDictionary<DataGridColumn, IDisposable> _column_hooks = new Dictionary<DataGridColumn, IDisposable>();
|
||||
private ScrollViewer _scroll_viewer;
|
||||
private ScrollBar _h_scroll, _v_scroll;
|
||||
private DataGridColumnHeadersPresenter _col_header_presenter;
|
||||
private IDictionary<DataGridColumn, IList<IDisposable>> _column_hooks = new Dictionary<DataGridColumn, IList<IDisposable>>();
|
||||
protected ScrollViewer _scroll_viewer;
|
||||
protected ScrollBar _h_scroll, _v_scroll;
|
||||
protected DataGridColumnHeadersPresenter _col_header_presenter;
|
||||
private double _horizontal_offset = 0d;
|
||||
|
||||
private void _grid_Loaded(object sender, RoutedEventArgs e) { _init(); }
|
||||
private void _grid_Unloaded(object sender, RoutedEventArgs e) { _dispose_hooks(); this.detach(); }
|
||||
|
||||
private void _init() {
|
||||
_dispose_hooks();
|
||||
_scroll_viewer = _grid.visual_children_where(fe => fe is ScrollViewer).Cast<ScrollViewer>().FirstOrDefault();
|
||||
@@ -37,25 +38,22 @@ namespace mbts.ui.controls
|
||||
_v_scroll = _grid.visual_children_where(fe => fe is ScrollBar).Cast<ScrollBar>().Where(s => s.Orientation == Orientation.Vertical).FirstOrDefault();
|
||||
// do not add hooks if any components are null (prevents null ref exceptions in OnRender)
|
||||
if (_scroll_viewer == null || _col_header_presenter == null || _h_scroll == null || _v_scroll == null) return;
|
||||
_grid_hooks.Add(_get_scrollbar_hook(_h_scroll));
|
||||
_grid_hooks.Add(_get_scrollbar_hook(_v_scroll));
|
||||
_grid_hooks.AddRange(get_scrollbar_hooks(_h_scroll));
|
||||
_grid_hooks.AddRange(get_scrollbar_hooks(_v_scroll));
|
||||
_grid_hooks.Add(_grid.ObserveProperty(DataGridEx.ActualHeightProperty, (s, e) => InvalidateVisual()));
|
||||
foreach (var c in _grid.Columns) _column_hooks.Add(c, _get_col_hook(c));
|
||||
_grid_hooks.AddRange(get_other_hooks());
|
||||
foreach (var c in _grid.Columns) _column_hooks.Add(c, get_col_hooks(c).ToList());
|
||||
_grid_hooks.Add(_grid.Columns.Subscribe(_columns_CollectionChanged));
|
||||
_scroll_viewer.ScrollChanged += _scroll_viewer_ScrollChanged;
|
||||
_grid_hooks.Add(Disposable.Create(() => _scroll_viewer.ScrollChanged -= _scroll_viewer_ScrollChanged));
|
||||
this.attach();
|
||||
InvalidateVisual();
|
||||
}
|
||||
|
||||
private void _columns_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
|
||||
switch (e.Action) {
|
||||
case NotifyCollectionChangedAction.Add: foreach (DataGridColumn c in e.NewItems) _column_hooks.Add(c, _get_col_hook(c)); break;
|
||||
case NotifyCollectionChangedAction.Remove:
|
||||
foreach (DataGridColumn c in e.OldItems) {
|
||||
IDisposable d;
|
||||
if (_column_hooks.TryGetValue(c, out d)) Disposable.dispose(ref d);
|
||||
}
|
||||
break;
|
||||
case NotifyCollectionChangedAction.Add: foreach (DataGridColumn c in e.NewItems) _column_hooks.Add(c, get_col_hooks(c).ToList()); break;
|
||||
case NotifyCollectionChangedAction.Remove: foreach (DataGridColumn c in e.OldItems) if (_column_hooks.TryGetValue(c, out IList<IDisposable> d)) Disposable.dispose(d); break;
|
||||
}
|
||||
InvalidateVisual();
|
||||
}
|
||||
@@ -67,29 +65,27 @@ namespace mbts.ui.controls
|
||||
_horizontal_offset = tt.X;
|
||||
}
|
||||
|
||||
private IDisposable _get_col_hook(DataGridColumn col) {
|
||||
return Disposable.Create(
|
||||
col.ObserveProperty(DataGridColumn.WidthProperty, (s, e) => InvalidateVisual()),
|
||||
col.ObserveProperty(DataGridColumn.VisibilityProperty, (s, e) => InvalidateVisual()),
|
||||
col.ObserveProperty(DataGridEx.LeftBorderProperty, (s, e) => InvalidateVisual()),
|
||||
col.ObserveProperty(DataGridEx.RightBorderProperty, (s, e) => InvalidateVisual())
|
||||
);
|
||||
}
|
||||
|
||||
private IDisposable _get_scrollbar_hook(ScrollBar sb) {
|
||||
return Disposable.Create(
|
||||
sb.ObserveProperty(ScrollBar.VisibilityProperty, (s, e) => InvalidateVisual()),
|
||||
sb.ObserveProperty(ScrollBar.ActualWidthProperty, (s, e) => InvalidateVisual()),
|
||||
sb.ObserveProperty(ScrollBar.ActualHeightProperty, (s, e) => InvalidateVisual())
|
||||
);
|
||||
}
|
||||
|
||||
private void _dispose_hooks() {
|
||||
Disposable.dispose(_grid_hooks);
|
||||
foreach (var hook in _column_hooks.Values) hook.Dispose();
|
||||
foreach (var hook in _column_hooks.Values) Disposable.dispose(hook);
|
||||
_column_hooks.Clear();
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<IDisposable> get_col_hooks(DataGridColumn col) {
|
||||
yield return col.ObserveProperty(DataGridColumn.WidthProperty, (s, e) => InvalidateVisual());
|
||||
yield return col.ObserveProperty(DataGridColumn.VisibilityProperty, (s, e) => InvalidateVisual());
|
||||
yield return col.ObserveProperty(DataGridEx.LeftBorderProperty, (s, e) => InvalidateVisual());
|
||||
yield return col.ObserveProperty(DataGridEx.RightBorderProperty, (s, e) => InvalidateVisual());
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<IDisposable> get_scrollbar_hooks(ScrollBar sb) {
|
||||
yield return sb.ObserveProperty(ScrollBar.VisibilityProperty, (s, e) => InvalidateVisual());
|
||||
yield return sb.ObserveProperty(ScrollBar.ActualWidthProperty, (s, e) => InvalidateVisual());
|
||||
yield return sb.ObserveProperty(ScrollBar.ActualHeightProperty, (s, e) => InvalidateVisual());
|
||||
}
|
||||
|
||||
protected virtual IEnumerable<IDisposable> get_other_hooks() { return Enumerable.Empty<IDisposable>(); }
|
||||
|
||||
protected override void OnRender(DrawingContext drawingContext) {
|
||||
double x = _grid.Columns.Take(_grid.FrozenColumnCount).Where(c => c.Visibility == Visibility.Visible).Select(c => c.ActualWidth).Sum();
|
||||
DataGridHeadersVisibility header_vis = _grid.HeadersVisibility;
|
||||
@@ -99,30 +95,25 @@ namespace mbts.ui.controls
|
||||
}
|
||||
foreach (var c in _grid.Columns.Skip(_grid.FrozenColumnCount).Where(c => c.Visibility == Visibility.Visible)) {
|
||||
var lb = DataGridEx.GetLeftBorder(c);
|
||||
if (lb != null) _draw_vertical_line(drawingContext, x, lb);
|
||||
if (lb != null) draw_vertical_line(drawingContext, x, lb);
|
||||
x += c.ActualWidth;
|
||||
var rb = DataGridEx.GetRightBorder(c);
|
||||
if (rb != null) _draw_vertical_line(drawingContext, x, rb);
|
||||
if (rb != null) draw_vertical_line(drawingContext, x, rb);
|
||||
}
|
||||
if (!(RenderTransform is TranslateTransform)) RenderTransform = new TranslateTransform(_horizontal_offset, 0d);
|
||||
}
|
||||
|
||||
//draw 1 continuous line if possible to help performance
|
||||
private void _draw_vertical_line(DrawingContext dc, double x, DataGridExVerticalLine line) {
|
||||
protected virtual void draw_vertical_line(DrawingContext dc, double x, DataGridExVerticalLine line) {
|
||||
if (line.through_colum_header) {
|
||||
if (line.through_cells)
|
||||
dc.DrawLine(line.pen.Value, new Point(x, 0), new Point(x, _grid.ActualHeight - _h_scroll.ActualHeight));
|
||||
else
|
||||
dc.DrawLine(line.pen.Value, new Point(x, 0), new Point(x, _grid.super_header_height + _col_header_presenter.ActualHeight));
|
||||
} else if (line.through_cells)
|
||||
_draw_through_cells(dc, x, line.pen.Value);
|
||||
dc.DrawLine(line.pen.Value, new Point(x, _grid.super_header_height + _col_header_presenter.ActualHeight), new Point(x, _grid.ActualHeight));
|
||||
}
|
||||
|
||||
private void _draw_through_cells(DrawingContext dc, double x, Pen pen) { dc.DrawLine(pen, new Point(x, _grid.super_header_height + _col_header_presenter.ActualHeight), new Point(x, _grid.ActualHeight - _h_scroll.ActualHeight)); }
|
||||
|
||||
public void Dispose() {
|
||||
_dispose_hooks();
|
||||
this.detach();
|
||||
}
|
||||
public void Dispose() { _dispose_hooks(); this.detach(); }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user