Professional Documents
Culture Documents
U2U WPF - 09 - Data Binding
U2U WPF - 09 - Data Binding
U2U WPF - 09 - Data Binding
Data Binding
U2U 2007
Data Binding
U2U 2007
8/28/2007
U2U 2007
Object
Property
Control
Property
U2U 2007
8/28/2007
Control to control
U2U 2007
Data Conversion
Bind to another type by converting it
Use Converter
8/28/2007
Building a Converter
Implement the IValueConverter interface
UpdateSourceTrigger
TextBox doesnt change value immediately
UpdateSourceTrigger values:
Default
LostFocus (= Default for TextBox)
PropertyChanged
Explicit
2000-2007 U2U Belgium - www.u2u.net
8/28/2007
Data Validation
Users will always make mistakes
Validation helps users determine these
Default DataBinding doesnt support this
Exception Validation
Data Source can throw Exception
10
8/28/2007
11
12
8/28/2007
13
14
8/28/2007
Using Styles
Even better, use a style for all textboxes
15
Using Tooltips
User cannot see what is wrong
Use a tooltip to show the actual error
16
8/28/2007
17
Use DataContext!
18
8/28/2007
19
20
10
8/28/2007
21
By Type
Type selects
DataTemplate
22
11
8/28/2007
PersonDataTemplateSelector
23
U2U 2007
24
Your class or
Service Proxy
(Optional)
Method to call
12
8/28/2007
25
26
Set IsSynchronizedWithCurrentItem="True"
13
8/28/2007
Master Detail
27
U2U 2007
DisplayMemberPath
Replaces single-field template
28
14
8/28/2007
29
CollectionViewSource in XAML
30
15
8/28/2007
31
Define a GroupStyle
Binding to XML
cars.xml
<XmlDataProvider
x:Key="cars"
XPath="/Cars/Car"
Source="cars.xml" />
32
<Cars>
<Car Make="Ford"
Model="F-150">
<Image>truck.png</Image>
</Car>
<Car> ... </Car>
</Cars>
<TextBlock
TextContent="{Binding XPath=@Make,
Source={StaticResource cars}}"
/>
16
8/28/2007
33
myGrid.DataContext = doc;
Binding To DataSets
Fill the DataSet as usual
Then use it as the DataContext
34
17
8/28/2007
Categories To Products
Building a Master-Detail
35
36
18
8/28/2007
Categories List
ListBox uses ItemsSource
37
And ItemTemplate
IsSynchronizedWithCurrentItem=True
2000-2007 U2U Belgium - www.u2u.net
Categories Detail
TextBoxes bind to same data
38
19
8/28/2007
Product List
ListBox is bound using DataRelation
39
ItemsSource is a CollectionViewSource
Product Detail
Uses a DataTemplate
40
20
8/28/2007
Product Detail
Use the ContentControl
41
Hierarchical Binding
42
<Foo xmlns=''>
<Quux>
<Wibble />
</Quux>
<Bar />
<Spong>
<Ick />
<Iyck/>
<Baz />
</Spong>
</Foo>
U2U 2007
21
8/28/2007
43
22