2013年8月3日星期六

UpdateSourceTrigger Property in WPF Binding

 

Introduction

 

this article I will describe in WPF and Silverlight update binding source concept . As you know, when we use TwoWay binding mode, any changes that occur on the target control will affect the binding source value.

 

Please note that only a TwoWay binding, rather than the default binding mode OneWay or OneTime.

 

Now, the question is when the update source data, because the target control's data changes. There are actually three ways to achieve.

 
      
         
    • Default: controlled by different control, such TextBox , when LostFocus event is triggered, the target binding changes.
    •    
    • PropertyChanged : means that when the target control value changes, the source data is immediately updated. example, TextBox is the target binding, when the input character, the source data is also changed. means when you enter a character created when, TextBox Text data is also changing.
    •    
    • Explicit: When UpdateSourceTrigger is set to Explicit , the data source is not automatically updated, and only after the code which shows the trigger.
    •   
     
 

 

these methods I wrote it for some code to help you better understand them, we first start from the default Look at the following code block:

 
  
 1 <Border BorderThickness="2" Margin="0 20 0 0" BorderBrush="Brown" CornerRadius="5"> 
2 <StackPanel>
3 <TextBox x:Name="textInput2" Margin="5" />
4 <TextBox x:Name="txtOutput2" Margin="5"
5 Text="{Binding Text,
6 ElementName=textInput2,
7 UpdateSourceTrigger=Default,
8 Mode=TwoWay}"/>
9 </StackPanel>
10 </Border>
 
 

 

Here's a Border element inside a StackPanel which contains two TextBox. second TextBox's Text property of a TextBox to bind it first property Text. In all cases there is actually some code that tied given Mode is set to TwoWay because we want to update the source data changes when both target and finally, being bound property UpdateSourceTrigger Set Default . This means that when the second TextBox loses focus or target binding triggers LostFocus event, the first one Text property of a TextBox to bind the source data is updated.

 

second example is set UpdateSourceTrigger as PropertyChanged .

 
  
 1 <Border BorderThickness="2" BorderBrush="Brown" CornerRadius="5"> 
2 <StackPanel>
3 <TextBox x:Name="textInput1" Margin="5" />
4 <TextBox x:Name="txtOutput1" Margin="5"
5 Text="{Binding Text,
6 ElementName=textInput1,
7 UpdateSourceTrigger=PropertyChanged,
8 Mode=TwoWay}"/>
9 </StackPanel>
10 </Border>
 
 

 

this code block, and the second compared to the only difference is UpdateSourceTrigger is set to PropertyChanged . As the name implies, this means that whenever the second TextBox's Text property changes, the first TextBox's Text property is updated immediately.

 

Finally, our last example is the attribute UpdateSourceTrigger set to Explicit < / code> .

 
  
 1 <Border BorderThickness="2" Margin="0 20 0 0" BorderBrush="Brown" CornerRadius="5"> 
2 <StackPanel>
3 <TextBox x:Name="textInput3" Margin="5" />
4 <TextBox x:Name="txtOutput3" Margin="5"
5 Text="{Binding Text,
6 ElementName=textInput3,
7 UpdateSourceTrigger=Explicit,
8 Mode=TwoWay}"/>
9 <Button Content="Save" Click="Button_Click"/>
10 </StackPanel>
11 </Border>
 
 

different things in this code is UpdateSourceTrigger set to Explicit . As we said earlier, this means that the update process will not happen unless you make clear operation. This Button is declared in the second TextBox behind responsible for doing clear operation. As you may have noticed, there is a click event EventHandler its Click event, the following lines of code are written in that EventHandler inside.

 
  
1 private void Button_Click(object sender, RoutedEventArgs e) 
2 {
3 BindingExpression be = txtOutput3.GetBindingExpression(TextBox.TextProperty);
4 be.UpdateSource();
5 }
 
 

BindingExpression class declares an instance. After that, txtOutput3 get TextBox. Text Property binding expression placed in that instance. Finally, UpdateSource method is known as the binding source explicitly update.

 

code download address: http://www.codeproject.com/Articles / 507883/UpdateSourceTrigger-Property-in-WPF-Binding

没有评论:

发表评论