Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UpdateSourceTrigger=Explicit

Tags:

binding

wpf

I'm creating a WPF window with multiple textboxes, when the user presses the OK button I want all the text boxes to be evaluated for being non-blank. I understand that I have to use TextBoxes with 'UpdateSourceTrigger of 'Explicit', but do I need to call 'UpdateSource()' for each of them ? e.g.

<TextBox Height="23" 
     HorizontalAlignment="Left" 
     Margin="206,108,0,0" 
     Text="{Binding Path=Definition, UpdateSourceTrigger=Explicit}"
     Name="tbDefinitionFolder" 
     VerticalAlignment="Top" 
     Width="120" />

<TextBox Height="23" 
     HorizontalAlignment="Left" 
     Margin="206,108,0,0" 
     Text="{Binding Path=Release, UpdateSourceTrigger=Explicit}"
     Name="tbReleaseFolder" 
     VerticalAlignment="Top" 
     Width="120" />

...

BindingExpression be = tbDefinitionFolder.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
BindingExpression be2 = tbReleaseFolder.GetBindingExpression(TextBox.TextProperty);
be2.UpdateSource();
like image 442
Chris Milburn Avatar asked Jan 10 '12 15:01

Chris Milburn


People also ask

What is UpdateSourceTrigger?

This is a property on a binding that controls the data flow from a target to a source and used for two-way databinding. The default mode is when the focus changes but there are many other options available, that we will see in this article.

How do you implement RaisePropertyChanged?

You must send your variable name to its parameter instead of string. Empty . You can prototype your RaisePropertyChanged method as void RaisePropertyChanged([CallerMemberName] string propertyName = null) and send in to it no parameters, so your caller name will be used to as a parameter value.

What is source and target in WPF?

The target object is the object that owns the property which we are binding to, i.e. the UI control rendering our data. The target property is the property that has been set via the markup extension, and the source property is the path of the binding.

What is data binding WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.


1 Answers

If you use Explicit you need to call UpdateSource.

I am not sure if this is the best approach to what you try to do though, i for one virtually never use Explicit, i rather bind to a copy of an object if i do not want changes to apply right away, or i store a copy and revert everything back if edits are to be cancelled.

like image 134
H.B. Avatar answered Oct 18 '22 18:10

H.B.