Reputation: 3187
Simple problem, let's say I have this TextBox (which, as a matter of fact, I do have):
<TextBox.Text>
<Binding Path="MySourceProperty"
Mode="OneWayToSource"
UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
It will succesfully update MySourceProperty whenever its Text property changes. What I want in addition to that is to MySourceProperty to get updated also when TextBox get focused.
Is there a way to have two UpdateSourceTriggers for one control?
For the details, I have a parent view containing a Label, bound to MySourceProperty, and another view, which contains a TextBox and a ComboBox.
So MySourceProperty should be updated when any of these events occur:
The last two events are obviously here because giving focus to a control doesn't change its Text or SelectedItem property, thus doesn't update MySourceProperty either.
EDIT: I've put the question in bold to be clear about what I want. I could solve the issue by using event handlers in code behind, but I'm trying hard to lose my WinForms habits. But if it is the only solution, feel free to answer with it.
Upvotes: 2
Views: 1708
Reputation: 1394
Not sure if this helps you Kilazur
I would give a try first as Erno mentioned, if that won't fit your scenario then I have done something similar to this.
TextBox Name="itemNameTextBox" Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}"
Then call the UpdateSource in respective events in code behind (which you mentioned above).
// itemNameTextBox is an instance of a TextBox
BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
Referred from here
Upvotes: 2
Reputation: 8786
Is there a way to have two UpdateSourceTriggers for one control?`
The answer to your question is no. And even if it dose, the other Trigger is LostFocus
.
If you are really like you said:
I could solve the issue by using event handlers in code behind, but I'm trying hard to lose my WinForms habits.
Then based on my understanding: you are using two control TextBox
and ComboBox
to update a single property MySourceProperty
. My suggestion is that you should use TwoWay
binding, or you can actually combine them by using ComboBox
with ComboBox.IsEditable
set to true, and data bind ComboBox.Text
and ComboBox.SelectedItem
with your MySourceProperty
.
Edit: If you do need to do it the way you are doing, then instead of using just one property, the better way of doing it is to bind TextBox.Text
and ComboBox.SelectedItem
to two different properties. Then your MySourceProperty
should return either of them based on their focus status.
Upvotes: 0
Reputation: 50712
You might want to put these controls that represent an entity in a single data template or user control. Then bind the datacontext of that control to the selected item of the combo box. The binding will now be relative to the selected item and will update automatically. No need to handle focus or multiple update triggers.
Upvotes: 0