/
AvalonHasChangeBehavior.cs
48 lines (43 loc) · 1.4 KB
/
AvalonHasChangeBehavior.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
using System;
using System.Windows;
using System.Windows.Interactivity;
using ICSharpCode.AvalonEdit;
namespace DocumentDbExplorer.Infrastructure.AvalonEdit
{
public sealed class AvalonHasChangeBehavior : Behavior<TextEditor>
{
private static readonly DependencyProperty HasChangeProperty =
DependencyProperty.Register("HasChange", typeof(bool), typeof(AvalonHasChangeBehavior));
public bool HasChange
{
get { return (bool)GetValue(HasChangeProperty); }
set { SetValue(HasChangeProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.TextChanged += AssociatedObjectOnTextChanged;
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
{
AssociatedObject.TextChanged -= AssociatedObjectOnTextChanged;
}
}
private void AssociatedObjectOnTextChanged(object sender, EventArgs eventArgs)
{
if (sender is TextEditor textEditor)
{
if (textEditor.Document != null)
{
HasChange = textEditor.Document.UndoStack.CanUndo;
}
}
}
}
}