-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathToggleButtonAttach.cs
70 lines (63 loc) · 2.6 KB
/
ToggleButtonAttach.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
68
69
70
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Controls.Primitives;
namespace AreaDesignWpfControls
{
public static class ToggleButtonAttach
{
#region IsAutoFold
[AttachedPropertyBrowsableForType(typeof(ToggleButton))]
public static bool GetIsAutoFold(ToggleButton control)
{
return (bool)control.GetValue(IsAutoFoldProperty);
}
public static void SetIsAutoFold(ToggleButton control, bool value)
{
control.SetValue(IsAutoFoldProperty, value);
}
/// <summary>
/// 为具有 ToggleButtonGorgeousThemeSwitchStyle 样式的 <see cref="ToggleButton"/> 设置是否启用自动折叠
/// </summary>
public static readonly DependencyProperty IsAutoFoldProperty =
DependencyProperty.RegisterAttached("IsAutoFold", typeof(bool), typeof(ToggleButtonAttach),
new PropertyMetadata(false, ToggleButtonChanged));
private static void ToggleButtonChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
if (o is not ToggleButton control)
return;
if ((bool)e.NewValue)
{
control.MouseLeave += Control_MouseLeave;
control.Checked += Control_Checked;
control.Unchecked += Control_Checked;
if (!control.IsMouseOver)
VisualStateManager.GoToState(control, control.IsChecked == true ? "MouseLeaveChecked" : "MouseLeaveUnChecked", false);
}
else
{
control.MouseLeave -= Control_MouseLeave;
control.Checked -= Control_Checked;
control.Unchecked -= Control_Checked;
VisualStateManager.GoToState(control, "MouseOver", false);
}
}
private static void Control_Checked(object sender, RoutedEventArgs e)
{
var control = (ToggleButton)sender;
if(control.IsMouseOver)
return;
VisualStateManager.GoToState(control, control.IsChecked == true ? "MouseLeaveChecked" : "MouseLeaveUnChecked", false);
}
private static void Control_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
var control = (ToggleButton)sender;
VisualStateManager.GoToState(control, control.IsChecked == true ? "MouseLeaveChecked" : "MouseLeaveUnChecked", false);
}
#endregion
}
}