网上很多介绍关于设计期绑定的文章, 但是Coding动态创建binding方式基本没有, 在项目开发过程中经常会用到的,在此做个简单的笔记,以做参考.
binding的好处网上介绍的一堆, 不重复了,Binding 理解INotifyPropertyChanged, ObservableCollection, BindingMode 是基础
几种控件和组件的绑定方式如下:
/*WinBindObject .cs*/
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
namespace Exio.NR.WPF.Test
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
19
/// Interaction logic for WinBindObject.xaml
20
/// </summary>
21
public partial class WinBindObject : Window
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
Entity ent1, ent2;
24
public WinBindObject()
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
InitializeComponent();
27
this.btnShowContent.Click +=new RoutedEventHandler(btnShowContent_Click);
28
InitData();
29
}
30
void btnShowContent_Click(object sender, RoutedEventArgs e)
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32
MessageBox.Show(ent1.ToString(),"Red Canvas");
33
MessageBox.Show(ent2.ToString(), "Blue Canvas");
34
}
35
void InitData()
36![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
37
//初始化数据
38
ent1 = new Entity()
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
ent2 = new Entity()
53![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
Canvas InitElement(Entity ent)
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
//绑定
70
Canvas c1 = new Canvas();
71![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
TextBox t = new TextBox();
73![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
t.SetBinding(TextBox.TextProperty, new Binding("Name")
{ Mode = BindingMode.OneWay });
74
t.Width = 60;
75
t.DataContext = ent;
76
c1.Children.Add(t);
77![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
78
TextBox tH = new TextBox();
79![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
ObservableCollection<ListItemData> data = new ObservableCollection<ListItemData>();
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
data.Add(new ListItemData()
{ Text = "Child", Value = TargetPerson.Child });
89![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
data.Add(new ListItemData()
{ Text = "Man", Value = TargetPerson.Man });
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
data.Add(new ListItemData()
{ Text = "Woman", Value = TargetPerson.Woman });
91![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
92
ComboBox cb = new ComboBox();
93
cb.Width = 50;
94
cb.SetValue(Canvas.TopProperty, 50.0);
95![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
cb.SetBinding(ComboBox.SelectedValueProperty, new Binding("Target")
{ Mode = BindingMode.TwoWay });
96
cb.DisplayMemberPath = "Text";
97
cb.SelectedValuePath = "Value";
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
c1.SetBinding(Canvas.TopProperty, new Binding("Top")
{ Mode = BindingMode.OneWay });
99![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
c1.SetBinding(Canvas.LeftProperty, new Binding("Left")
{ Mode = BindingMode.OneTime });
100![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
c1.SetBinding(Canvas.WidthProperty, new Binding("Width")
{ Mode = BindingMode.OneTime });
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
c1.SetBinding(Canvas.HeightProperty, new Binding("Height")
{ Mode = BindingMode.OneTime });
102
cb.ItemsSource = data;
103
cb.DataContext = ent;
104
c1.Children.Add(cb);
105![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
106
TransformGroup tg = new TransformGroup();
107
c1.RenderTransform = tg;
108
//旋转角度, 注意这个绑定(因为RotateTransform 不是 FrameworkElement,没有SetBinding方法)
109
RotateTransform rt = new RotateTransform();
110![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
118![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public class ListItemData
{
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Text
{ get; set; }
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public TargetPerson Value
{ get; set; }
121
}
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
123
/// 使用群体
124
/// </summary>
125
public enum TargetPerson
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
127
Child,
128
Man,
129
Woman
130
}
131
public class Entity:INotifyPropertyChanged
132![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
133
public event PropertyChangedEventHandler PropertyChanged;
134
protected void Notify(string propName)
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
if (this.PropertyChanged != null)
137![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
138
PropertyChanged(this, new PropertyChangedEventArgs(propName));
139
}
140
}
141
string _name;
142![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
143
public string Name
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _name; }
146![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{
147![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._name == value)
{ return; }
148
this._name = value;
149
Notify("Name"); }
150
}
151
//旋转角度
152
double _angle;
153![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
public double Angle
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _angle; }
157
set
158![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
159![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._angle == value)
{ return; }
160
this._angle = value;
161
Notify("Angle");
162
}
163
}
164
double _top;
165![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
166
public double Top
167![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
168![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _top; }
169
set
170![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
171![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._top == value)
{ return; }
172
this._top = value;
173
Notify("Top");
174
}
175
}
176
double _left;
177![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
178
public double Left
179![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _left; }
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _left = value; }
182
}
183
double _width;
184![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
185
public double Width
186![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
187![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _width; }
188
set
189![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
190![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._width == value)
{ return; }
191
this._width = value;
192
Notify("Width");
193
}
194
}
195
double _height;
196![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
197
public double Height
198![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _height; }
200
set
201![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
202![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._height == value)
{ return; }
203
this._height = value;
204
Notify("Height");
205
}
206
}
207
Color _bg;
208![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
209
public Color Bacground
210![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
211![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _bg; }
212
set
213![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
214![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._bg == value)
{ return; }
215
this._bg = value;
216
Notify("Bacground");
217
}
218
}
219
//所属类别
220
TargetPerson _target;
221![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
222
public TargetPerson Target
223![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
224![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _target; }
225
set
226![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
227![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (this._target == value)
{ return; }
228
this._target = value;
229
Notify("Target");
230
}
231
}
232
public string ToString()
233![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
修改所有属性后点击按钮, 体会BindingMode
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
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>