Search This Blog

Monday, October 28, 2013

Data Binding in WPF

WPF  Data Binding

With Data binding in WPF you can take data from any property of an object and bind it to other dependency property of another object.
Data binding copies a value from a source object to a property on a destination object.
WPF object has change notification.
When the source change, the destination property will automatically be updated.

Simple example:

<Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox Name="Source" Grid.Row="0" >Source</TextBox>
        <TextBlock Grid.Row="1">
            <TextBlock.Text>
                <Binding ElementName="Source" Path="Text"  />
            </TextBlock.Text>
        </TextBlock>
</Grid>

Binding modes

OneWay
  • source -> destination
OneWayToSource
  • destination - > source
OneWayToSource
  • destination - > source
OneTime

  • source - > destination (the property is initially set, but updates to the source are not copied to the destination)
TwoWay

  • source -> destination
  • destination - > source
Other examples:

SQL Server datagrid view,update, delete data