/
TreeToggleButton.cs
67 lines (59 loc) · 1.94 KB
/
TreeToggleButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows;
using Eto.Forms;
using System.Windows.Controls;
using Eto.Platform.CustomControls;
namespace Eto.Platform.Wpf.CustomControls.TreeGridView
{
public class TreeToggleButton : ToggleButton
{
public const int LEVEL_WIDTH = 16;
public TreeController Controller { get; set; }
public ITreeGridItem Item { get; private set; }
static TreeToggleButton ()
{
DefaultStyleKeyProperty.OverrideMetadata (typeof (TreeToggleButton), new FrameworkPropertyMetadata (typeof (TreeToggleButton)));
}
public static FrameworkElement Create (FrameworkElement content, TreeController controller)
{
var panel = new StackPanel { Orientation = Orientation.Horizontal };
var button = new TreeToggleButton { Controller = controller, Width = 16 };
panel.Children.Add (button);
panel.DataContextChanged += (sender, e) => {
button.Configure (panel.DataContext as ITreeGridItem);
};
panel.Children.Add (content);
return panel;
}
protected override void OnPreviewMouseLeftButtonDown (System.Windows.Input.MouseButtonEventArgs e)
{
base.OnPreviewMouseLeftButtonDown (e);
var index = Controller.IndexOf ((ITreeGridItem)this.DataContext);
if (index >= 0) {
Dispatcher.BeginInvoke (new Action (delegate {
if (this.IsChecked ?? false) {
if (Controller.CollapseRow (index)) {
this.IsChecked = false;
}
}
else if (Controller.ExpandRow (index)) {
this.IsChecked = true;
}
}));
}
e.Handled = true;
}
public void Configure (ITreeGridItem item)
{
this.Item = item;
var index = Controller.IndexOf (item);
this.IsChecked = Controller.IsExpanded (index);
this.Visibility = item != null && item.Expandable ? Visibility.Visible : Visibility.Hidden;
this.Margin = new Thickness (Controller.LevelAtRow (index) * LEVEL_WIDTH, 0, 0, 0);
}
}
}