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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
namespace FLY.ControlLibrary
{
/// <summary>
/// 后台添加功能,按钮按下 时 属性=true, 抬起属性=false
/// </summary>
public class ResetAction2 : IDisposable
{
/// <summary>
/// 对象
/// </summary>
public object obj;
/// <summary>
/// bool 属性
/// </summary>
public string propertyname;
/// <summary>
/// 按下时为 true
/// </summary>
public bool pressIsTrue;
UIElement uIElement;
/// <summary>
///
/// </summary>
/// <param name="uIElement"></param>
/// <param name="obj"></param>
/// <param name="propertyname"></param>
/// <param name="pressIsTrue"></param>
public ResetAction2(UIElement uIElement, object obj, string propertyname, bool pressIsTrue = true)
{
this.uIElement = uIElement;
this.obj = obj;
this.propertyname = propertyname;
this.pressIsTrue = pressIsTrue;
uIElement.PreviewMouseDown += UIElement_PreviewMouseDown;
uIElement.PreviewTouchDown += UIElement_PreviewTouchDown;
uIElement.PreviewMouseUp += UIElement_PreviewMouseUp;
uIElement.PreviewTouchUp += UIElement_PreviewTouchUp;
}
/// <summary>
///
/// </summary>
/// <param name="uIElement"></param>
/// <param name="obj"></param>
/// <param name="propertyname"></param>
/// <param name="pressIsTrue"></param>
public ResetAction2(UIElement uIElement, string propertyname, bool pressIsTrue = true)
{
this.uIElement = uIElement;
this.propertyname = propertyname;
this.pressIsTrue = pressIsTrue;
uIElement.PreviewMouseDown += UIElement_PreviewMouseDown;
uIElement.PreviewTouchDown += UIElement_PreviewTouchDown;
uIElement.PreviewMouseUp += UIElement_PreviewMouseUp;
uIElement.PreviewTouchUp += UIElement_PreviewTouchUp;
}
public void Dispose()
{
uIElement.PreviewMouseDown -= UIElement_PreviewMouseDown;
uIElement.PreviewTouchDown -= UIElement_PreviewTouchDown;
uIElement.PreviewMouseUp -= UIElement_PreviewMouseUp;
uIElement.PreviewTouchUp -= UIElement_PreviewTouchUp;
}
private void UIElement_PreviewTouchUp(object sender, TouchEventArgs e)
{
Up();
}
private void UIElement_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
Up();
}
private void UIElement_PreviewTouchDown(object sender, TouchEventArgs e)
{
Down();
}
private void UIElement_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Down();
}
bool IsObjNull()
{
if (obj == null)
{
if (uIElement is FrameworkElement)
{
obj = (uIElement as FrameworkElement).DataContext;
if (obj == null)
return true;
}
else
{
return true;
}
}
return false;
}
void Down()
{
if (IsObjNull()) return;
if (pressIsTrue)
Misc.PropertiesManager.SetValue(obj, propertyname, true);
else
Misc.PropertiesManager.SetValue(obj, propertyname, false);
}
void Up()
{
if (IsObjNull()) return;
if (pressIsTrue)
Misc.PropertiesManager.SetValue(obj, propertyname, false);
else
Misc.PropertiesManager.SetValue(obj, propertyname, true);
}
}
/// <summary>
/// ResetAction2 的 XAML 版 ,附加行为。
/// 与 NoToggleButton 控件 一起使用
/// </summary>
public class ResetBehavior : Behavior<UIElement>
{
public System.Windows.Data.Binding Binding { get; set; }
ResetAction2 resetAction = null;
public ResetBehavior()
{
}
protected override void OnAttached()
{
base.OnAttached();
UIElement dobj = AssociatedObject;
if (Binding != null && Binding.Path != null)
{
if (Binding.Source != null)
{
resetAction = new ResetAction2(dobj, Binding.Source, Binding.Path.Path);
}
else
{
var obj = COMMON.GetDataContext(dobj);
if (obj == null)
{
resetAction = new ResetAction2(dobj, Binding.Path.Path);
}
else
{
resetAction = new ResetAction2(dobj, obj, Binding.Path.Path);
}
}
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (resetAction != null)
{
resetAction.Dispose();
resetAction = null;
}
}
}
}