网上很多介绍关于设计期绑定的文章, 但是Coding动态创建binding方式基本没有, 在项目开发过程中经常会用到的,在此做个简单的笔记,以做参考.
binding的好处网上介绍的一堆, 不重复了,Binding 理解INotifyPropertyChanged, ObservableCollection, BindingMode 是基础
几种控件和组件的绑定方式如下:
/*WinBindObject .cs*/

WinBindObject .cs
1
using System;
2
using System.Collections.Generic;
3
using System.Collections.ObjectModel;
4
using System.Linq;
5
using System.Text;
6
using System.Windows;
7
using System.Windows.Controls;
8
using System.Windows.Data;
9
using System.Windows.Documents;
10
using System.Windows.Input;
11
using System.Windows.Media;
12
using System.Windows.Media.Imaging;
13
using System.Windows.Shapes;
14
using System.ComponentModel;
15
16
namespace Exio.NR.WPF.Test
17

{
18
/**//// <summary>
19
/// Interaction logic for WinBindObject.xaml
20
/// </summary>
21
public partial class WinBindObject : Window
22
{
23
Entity ent1, ent2;
24
public WinBindObject()
25
{
26
InitializeComponent();
27
this.btnShowContent.Click +=new RoutedEventHandler(btnShowContent_Click);
28
InitData();
29
}
30
void btnShowContent_Click(object sender, RoutedEventArgs e)
31
{
32
MessageBox.Show(ent1.ToString(),"Red Canvas");
33
MessageBox.Show(ent2.ToString(), "Blue Canvas");
34
}
35
void InitData()
36
{
37
//初始化数据
38
ent1 = new Entity()
39
{
40
Name="Knife",
41
Angle = 10,
42
Top = 30,
43
Left =30,
44
Height = 72,
45
Width = 80,
46
Bacground = Colors.Red,
47
Target = TargetPerson.Man
48
};
49
Canvas c1 = InitElement(ent1);
50
canvContainer.Children.Add(c1);
51
52
ent2 = new Entity()
53
{
54
Name = "Robot",
55
Angle = -60,
56
Top = 160,
57
Left = 90,
58
Height =42,
59
Width = 80,
60
Bacground = Colors.Blue,
61
Target = TargetPerson.Child
62
};
63
Canvas c2 = InitElement(ent2);
64
canvContainer.Children.Add(c2);
65
}
66
67
Canvas InitElement(Entity ent)
68
{
69
//绑定
70
Canvas c1 = new Canvas();
71
72
TextBox t = new TextBox();
73
t.SetBinding(TextBox.TextProperty, new Binding("Name")
{ Mode = BindingMode.OneWay });
74
t.Width = 60;
75
t.DataContext = ent;
76
c1.Children.Add(t);
77
78
TextBox tH = new TextBox();
79
tH.SetBinding(TextBox.TextProperty, new Binding("Top")
{ Mode = BindingMode.TwoWay });
80
tH.Width = 60;
81
tH.SetValue(Canvas.TopProperty, 25.0);
82
tH.DataContext = ent;
83
c1.Children.Add(tH);
84
85
86
87
ObservableCollection<ListItemData> data = new ObservableCollection<ListItemData>();
88
data.Add(new ListItemData()
{ Text = "Child", Value = TargetPerson.Child });
89
data.Add(new ListItemData()
{ Text = "Man", Value = TargetPerson.Man });
90
data.Add(new ListItemData()
{ Text = "Woman", Value = TargetPerson.Woman });
91
92
ComboBox cb = new ComboBox();
93
cb.Width = 50;
94
cb.SetValue(Canvas.TopProperty, 50.0);
95
cb.SetBinding(ComboBox.SelectedValueProperty, new Binding("Target")
{ Mode = BindingMode.TwoWay });
96
cb.DisplayMemberPath = "Text";
97
cb.SelectedValuePath = "Value";
98
c1.SetBinding(Canvas.TopProperty, new Binding("Top")
{ Mode = BindingMode.OneWay });
99
c1.SetBinding(Canvas.LeftProperty, new Binding("Left")
{ Mode = BindingMode.OneTime });
100
c1.SetBinding(Canvas.WidthProperty, new Binding("Width")
{ Mode = BindingMode.OneTime });
101
c1.SetBinding(Canvas.HeightProperty, new Binding("Height")
{ Mode = BindingMode.OneTime });
102
cb.ItemsSource = data;
103
cb.DataContext = ent;
104
c1.Children.Add(cb);
105
106
TransformGroup tg = new TransformGroup();
107
c1.RenderTransform = tg;
108
//旋转角度, 注意这个绑定(因为RotateTransform 不是 FrameworkElement,没有SetBinding方法)
109
RotateTransform rt = new RotateTransform();
110
BindingOperations.SetBinding(rt, RotateTransform.AngleProperty, new Binding("Angle")
{ Mode = BindingMode.OneTime });
111
tg.Children.Add(rt);
112
c1.Background = new SolidColorBrush(ent.Bacground);
113
c1.DataContext = ent;
114
return c1;
115
}
116
}
117
118
public class ListItemData
{
119
public string Text
{ get; set; }
120
public TargetPerson Value
{ get; set; }
121
}
122
/**//// <summary>
123
/// 使用群体
124
/// </summary>
125
public enum TargetPerson
126
{
127
Child,
128
Man,
129
Woman
130
}
131
public class Entity:INotifyPropertyChanged
132
{
133
public event PropertyChangedEventHandler PropertyChanged;
134
protected void Notify(string propName)
135
{
136
if (this.PropertyChanged != null)
137
{
138
PropertyChanged(this, new PropertyChangedEventArgs(propName));
139
}
140
}
141
string _name;
142
143
public string Name
144
{
145
get
{ return _name; }
146
set
{
147
if (this._name == value)
{ return; }
148
this._name = value;
149
Notify("Name"); }
150
}
151
//旋转角度
152
double _angle;
153
154
public double Angle
155
{
156
get
{ return _angle; }
157
set
158
{
159
if (this._angle == value)
{ return; }
160
this._angle = value;
161
Notify("Angle");
162
}
163
}
164
double _top;
165
166
public double Top
167
{
168
get
{ return _top; }
169
set
170
{
171
if (this._top == value)
{ return; }
172
this._top = value;
173
Notify("Top");
174
}
175
}
176
double _left;
177
178
public double Left
179
{
180
get
{ return _left; }
181
set
{ _left = value; }
182
}
183
double _width;
184
185
public double Width
186
{
187
get
{ return _width; }
188
set
189
{
190
if (this._width == value)
{ return; }
191
this._width = value;
192
Notify("Width");
193
}
194
}
195
double _height;
196
197
public double Height
198
{
199
get
{ return _height; }
200
set
201
{
202
if (this._height == value)
{ return; }
203
this._height = value;
204
Notify("Height");
205
}
206
}
207
Color _bg;
208
209
public Color Bacground
210
{
211
get
{ return _bg; }
212
set
213
{
214
if (this._bg == value)
{ return; }
215
this._bg = value;
216
Notify("Bacground");
217
}
218
}
219
//所属类别
220
TargetPerson _target;
221
222
public TargetPerson Target
223
{
224
get
{ return _target; }
225
set
226
{
227
if (this._target == value)
{ return; }
228
this._target = value;
229
Notify("Target");
230
}
231
}
232
public string ToString()
233
{
234
return string.Format(@"
235
Name :{0}
236
Angle :{1}
237
Top :{2}
238
Left :{3}
239
Width :{4}
240
Height :{5}
241
Bacground:{6}
242
Target :{7}",
243
Name ,
244
Angle ,
245
Top ,
246
Left ,
247
Width ,
248
Height ,
249
Bacground,
250
Target);
251
}
252
}
253
}
254
修改所有属性后点击按钮, 体会BindingMode

WinBindObject.xaml
1
<Window x:Class="Exio.NR.WPF.Test.WinBindObject"
2
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
Title="WinBindObject" Height="300" Width="300">
5
<Canvas x:Name="canvContainer">
6
</Canvas>
7
</Window>