new Adorner to convey row selection
This commit is contained in:
1 parent
540559de3e
commit
decdae424d
1 file changed
+93
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using bts.utils;
|
||||
using mbts.ui.extensions;
|
||||
|
||||
namespace mbts.ui.controls.adorners {
|
||||
public class RowSelectionAdorner : Adorner, DIYGrid.ISelectionHighlightListener {
|
||||
Brush _fill = DIYGrid.DefaultSelectionHighlight;
|
||||
Pen _stroke = DIYGrid.DefaultSelectionStroke;
|
||||
|
||||
DIYGridRowsPresenter _diy_rp;
|
||||
ScrollViewer _diy_scroll;
|
||||
ScrollContentPresenter _diy_scp;
|
||||
|
||||
#region Fill
|
||||
public static readonly DependencyProperty FillProperty =
|
||||
WPFHelper.create_dp<Brush, RowSelectionAdorner>(
|
||||
nameof(Fill),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
(o, v) => o._fill = v,
|
||||
DIYGrid.DefaultSelectionHighlight);
|
||||
|
||||
public SolidColorBrush Fill {
|
||||
get => GetValue(FillProperty) as SolidColorBrush;
|
||||
set => SetValue(FillProperty, value);
|
||||
}
|
||||
#endregion
|
||||
#region Stroke
|
||||
public static readonly DependencyProperty StrokeProperty =
|
||||
WPFHelper.create_dp<Pen, RowSelectionAdorner>(
|
||||
nameof(Stroke),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
(o, v) => o._stroke = v,
|
||||
DIYGrid.DefaultSelectionStroke);
|
||||
|
||||
public Pen Stroke {
|
||||
get => GetValue(StrokeProperty) as Pen;
|
||||
set => SetValue(StrokeProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public RowSelectionAdorner(FrameworkElement adorned_element, ScrollViewer scroll_viewer) : base(adorned_element) {
|
||||
// we want our clicks to go through to the underlying grid
|
||||
IsHitTestVisible = false;
|
||||
|
||||
_diy_rp = (DIYGridRowsPresenter)adorned_element;
|
||||
_diy_scroll = scroll_viewer;
|
||||
_diy_scp = _diy_rp.descendants_of_type<ScrollContentPresenter>().empty_if_null().FirstOrDefault();
|
||||
|
||||
// so that we clear out any previously selected highlighted rows
|
||||
_diy_rp.SubscribePreviewMouseDown((s, e) => InvalidateVisual());
|
||||
// the main event
|
||||
_diy_rp.SubscribePreviewMouseUp((s, e) => InvalidateVisual());
|
||||
// so that we update the highlight as the grid scrolls
|
||||
_diy_scroll.SubscribeScrollChanged((s, e) => InvalidateVisual());
|
||||
}
|
||||
|
||||
|
||||
protected override void OnRender(DrawingContext drawingContext) {
|
||||
base.OnRender(drawingContext);
|
||||
if (AdornedElement is ListBox lb && lb.SelectedItems != null && lb.SelectedItems.Count > 0) {
|
||||
foreach(var lbi in lb.SelectedItems) {
|
||||
if (_diy_rp.ItemContainerGenerator.ContainerFromItem(lbi) is FrameworkElement row && row.IsVisible) {
|
||||
var top_left = lb.GetChildTopLeft(row);
|
||||
var top_is_within_bounds = top_left.Y >= 0;
|
||||
|
||||
// Note that the top of the row can still technically be visible but we wont show
|
||||
// the adorner rectangle b/c the bottom of the row will be obscured by the scrollbar
|
||||
var bottom_is_within_bounds = lb.GetChildBounds(row).BottomRight.Y <= _diy_scroll.ActualHeight;
|
||||
if (top_is_within_bounds && bottom_is_within_bounds) {
|
||||
drawingContext.DrawRectangle(_fill,
|
||||
_stroke,
|
||||
new Rect(top_left, new Point(top_left.X + row.ActualWidth, top_left.Y + row.ActualHeight)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void on_selection_fill(Brush selection_fill) {
|
||||
var selection_brush = (SolidColorBrush)selection_fill;
|
||||
Fill = selection_brush;
|
||||
Stroke = new Pen(selection_brush.Color.to_solid_brush(), 1);
|
||||
InvalidateVisual();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user