Professional Documents
Culture Documents
6460a 04
6460a 04
Windows® Presentation
Foundation
Module 4: Data Binding
• Overview of Data Binding
• Converting Data
• Validating Data
Lesson: Overview of Data Binding
• The WPF Data-Binding Model
• Data-Binding Modes
The WPF Data-Binding Model
Managed Object
XML Data
UI Element
Binding Sources and Binding Targets
Data-binding components:
Dependency
Property
Property
Data-Binding Modes
• OneWay • OneWayToSource
• TwoWay • OneTime
Binding Object
OneWay
TwoWay
OneWayToSource
OneTime
<DockPanel xmlns:c="clr-namespace:MyNamespace">
<DockPanel.Resources>
<c:MyClass x:Key="mySource" /> Path
</DockPanel.Resources>
<Button Background=
"{Binding Path=ColorName,
Source={StaticResource mySource}}">
...
</Button>
Source
</DockPanel>
Binding Multiple Controls to a Class
<DockPanel xmlns:c="clr-namespace:MyNamespace">
<DockPanel.Resources>...</DockPanel.Resources>
<DockPanel.DataContext>
<Binding Source="{StaticResource mySource}"/> Source
</DockPanel.DataContext>
<Button Background="{Binding Path=BackColorName}">
...
</Button>
<TextBox Foreground="{Binding Path=ForeColorName}">
...
</TextBox> Path
</DockPanel>
Binding to a Full Object
<DockPanel
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<DockPanel.Resources>
<sys:String x:Key="myData">Hello World!</sys:String>
</DockPanel.Resources>
<DockPanel.DataContext> Source
<Binding Source="{StaticResource myData}"/>
</DockPanel.DataContext>
<Label Content="{Binding}">
</DockPanel> Empty binding
syntax
Binding to XML Data
<ListBox> Source
<ListBox.ItemsSource>
<Binding Source="{StaticResource inventoryData}"
XPath="*[@Stock='out'] | *[@Number>=8 or
@Number=3]"/>
</ListBox.ItemsSource> XPath query
...
<TextBlock Text="{Binding XPath=Title}">
<TextBlock.Tooltip>
<TextBlock
Text="{Binding Path=Attributes[0].Value}" />
</TextxBlock.Tooltip>
</TextBlock>
... Node binding
</ListBox>
Binding to Another User Interface Element
To bind to a UI element:
Source
element
<StackPanel>
<ComboBox x:Name="myComboBox" SelectedIndex="0">
...
</ComboBox>
<Canvas Background="{Binding
ElementName=myComboBox, Path=SelectedItem.Content}"
Height="100"
Width="100" />
Path
</StackPanel>
ElementName
Demonstration: Binding to Different Data Sources
In this demonstration, you will see how to:
• Bind to a class property
Binding Object
OneWay
Binding TwoWay Binding
Target Source
OneWayToSource
UpdateSourceTrigger
Propagating Property Change Notifications to a
Binding Target
• Default
• Explicit
• LostFocus
• PropertyChanged
<TextBox Width="100">
<TextBox.Text>
<Binding Source="{StaticResource myData}"
Path="ColorName"
UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
Demonstration: Triggering Source Updates
In this demonstration, you will see how to:
• Propagate changes to the binding target
Background ColorName
Property ColorBrush- Property
Converter
(of type Brush) (of type String)
Lesson: Validating Data
• Default Data Validation
Dependency
Validation Converter Property
Property
<Binding.ValidationRules>
<ExceptionValidationRule />
</Binding.ValidationRules>
Providing Visual Validation Feedback
<ControlTemplate x:Key="errorTemplate">
<DockPanel>
<TextBlock Foreground="Red">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<TextBox Validation.ErrorTemplate =
"{StaticResource errorTemplate}" ...>
Defining a Custom Validation Rule
<TextBox xmlns:src="clr-namespace:MySample">
<TextBox.Text>
<Binding ...> ValidationRules
<Binding.ValidationRules>
<src:FutureDateRule />
</Binding.ValidationRules>
</Binding> Custom validation
</TextBox.Text> class
</TextBox>
Lab: Implementing a Data-Bound Application
• Exercise 1: Creating Data Bindings
Logon information
Virtual machine 6460A-LON-DEV-04
Password Pa$$w0rd
• Best Practices