2
votes

I have a textbox that contain a string that must be binded only when the user press the button. In XAML:

<Button Command="{Binding Path=PingCommand}" Click="Button_Click">Go</Button>
<TextBox x:Name="txtUrl" Text="{Binding Path=Url,UpdateSourceTrigger=Explicit, Mode=OneWay}" />

In the code-behind:

private void Button_Click(object sender, RoutedEventArgs e)
{  
    BindingExpression be = this.txtUrl.GetBindingExpression(TextBox.TextProperty);
    be.UpdateTarget();
}

"be" is always NULL. Why?

Update:

Alright, here is some update after a lot of try.

IF, I set the Mode the OneWay with Explicit update. I have a NullReferenceException in the "be" object from the GetBindingExpression.

IF, I set the Mode to nothing (default TwoWay) with Explicit update. I have the binding getting the value (string.empty) and it erases every times everything in the textbox.

IF, I set the Mode to OneWay, with PropertyChanged I have nothing raised by the property binded when I press keys in the textbox and once I click the button, I have a NULLReferenceException in the "be" object.

IF, I set the Mode to nothing (default TwoWay), with PropertyChanged I have the property that raise everytime I press (GOOD) but I do not want to have the property change everytime the user press a key... but only once the user press the Button.

3
be is not null for me... - Arsen Mkrtchyan
It not null the first time I click the button. The second time it is. I do not remove the binding explicitly this is why I am confused if some behavior can remove a binding that I might not know. - Patrick Desjardins
So this works a single time? Also, chance your question so that it doesn't say it's 'always' null. - Will Eddins
I am getting no null for the first, and second and thired and ... - Arsen Mkrtchyan
I thought initially it was all the time but I am trying so many thing that it seems to work the first time. BUT, now since I have put back the binding to Explicit the first, second and other try doesn't work anymore. Few minutes ago the "be" was null and crashing, now the binding doesn't work but "be" is not anymore null! Arggg - Patrick Desjardins

3 Answers

7
votes

Alright, after thinking a little more I noticed that :

 BindingExpression be = this.txtUrl.GetBindingExpression(TextBox.TextProperty);
 be.UpdateTarget();

Has something illogical because I do not want to update the Target but the source. I have simply changed be.Updatetarget() to be.UpdateSource(); and everything worked with this XAML:

 <TextBox x:Name="txtUrl" Text="{Binding Path=Url, UpdateSourceTrigger=Explicit}">...

Thank you to everybody who have helped me in the process to solve this problem. I have added +1 to everybody! Thanks

1
votes

It should work, may be you are calling functions that delete the binding before the lines you provide in event handler? Assigning value to text property of text box will remove binding

1
votes

Have you tried your data-binding without the UpdateSourceTrigger? If the binding failed, I would expect null to be the result of GetBindingExpression.

One cause of this could be that Url was not implemented as a DependencyProperty.