WPF Reflections

Mar 6 2008   2:45PM GMT

Explicit control of how the source is updated

Mark Shurmer Profile: MarkWPF

In a previous post, I described how you can get a binding to update in real time – i.e. as you type.
Sometimes however, you want to control the updating more closely, i.e. in code. For example, you may want to do the update only when a timer has ticked.
There is a little used argument for UpdateSourceTrigger (well in 18 months of using WPF I’ve never seen it used :-)) , namely Explicit that allows you to do that.
Given some xaml like:

<ComboBox Name=”cbUsers” SelectedValuePath=”Name” />
<TextBox Name=”tbUserName” Text=”{Binding ElementName=cbUsers, Path=SelectedValue}” />

When you add UpdateSourceTrigger=Explicit to the TextBox, you can control the updating by telling the binding mechanism to update.
<TextBox Name=”tbUserName” Text=”{Binding ElementName=cbUsers, Path=SelectedValue, UpdateSourceTrigger=Explicit}” />
How do you do that? That’s where the magic lies :-)
You need to get the BindingExpression for the TextBox:

BindingExpression binding = tbUserName.GetBindingExpression(TextBox.TextProperty);

Then update the linked source( the textbox):

binding.UpdateSource();

bingo

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: