Binding Object Properties – Mode

The binding mode sets in which direction data will flow. Data usually flows from the source to the destination. There are five data binding modes.

Mode Description Direction of Flow
OneWayToSource From destination to source .
OneWay From source to destination.
OneTime The property is initially set, but updates to the source are not copied to the destination.
TwoWay Changes to source and destination are copied to each other.
Default Depends on the UI control.  

The default Mode varies between one-way and two-way depending on the dependency property that is being bound. You can always declare the binding mode explicitly to ensure that your binding has the desired behavior. In general, user-editable control properties, such as TextBox.Text and RangeBase.Value, default to two-way bindings, whereas most other properties default to one-way bindings.

The following code explains the concepts in details.

<Window x:Class="Bindingtoclasses.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        Title="MainWindow" Height="350" Width="525">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Name="TextBox1" Grid.Row="0" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=TwoWay}"/>
        <TextBox Name="TextBox2" Grid.Row="1" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=OneWayToSource}"></TextBox>
        <TextBox Name="TextBox3" Grid.Row="2" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <TextBox Name="TextBox4" Grid.Row="3" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=OneWay}"></TextBox>
        <TextBlock Name="TextBlock1" Grid.Row="4" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=OneTime}"/>
        <TextBlock Name="TextBlock2" Grid.Row="5" Text="{Binding Path=AnotherField, Mode=TwoWay}"/>
        
    </Grid>
</Window>

  • TextBox1 binds TwoWay, so it updates the source whenever the content changes as well as it gets updated when the source is changed (for ex, from TextBox2. It updates the source when the control loses focus.
  • TextBox2 binds OneWayToSource, so it gets populated once and this textbox never gets updated when the source changes. When the user types in the textbox, it updates the source with the value that it holds.
  • TextBox3 binds TwoWay, so it updates the source whenever the content changes as well as it gets updated when the source is changed . The difference between TextBox1 & TextBox3 is that, TextBox1 gets updates the source when the control loses focus and TextBox3 updates the source as the user types in because of UpdateSourceTrigger.
  • TextBox3 binds OneWay so it never updates the source. The data moves from source to target. So when the source is changed in any way, this control gets updated.

Download the source code from here.

"The older I get the less I listen to what people say and the more I look at what they do."— Andrew Carnegie

1 thought on “Binding Object Properties – Mode

  1. Pingback: Introduction to Multi Binding and Multi value converter using IMultiValueConverter & MultiBinding « Kishore – Software Architect, Washington DC

Leave a comment