tags:

views:

1375

answers:

3

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}"></TextBox>

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.

+1  A: 

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

ArsenMkrt
I will check if somewhere in the code the value is assigned but I doubt it because it's all binded driven design.
Daok
+1  A: 

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.

Will Eddins
Url is not a DependencyProperty. Should it be?
Daok
Yes, there's a good chance this will fix your issue.
Will Eddins
On second though, for OneWay binding, I'm not sure...
Will Eddins
The source of a `Binding` does not have to be a `DependencyProperty`, it can be a normal property. Only the target of the `Binding` must be a `DP`.
Joel B Fant
I have changed the binding to UpdateSourceTrigger=PropertyChanged and without assigning a Mode. I see that the property behind is changing everytime I type. Now, I have put back to Explicit (without changing the mode so it's the default 2ways one), and the property doesn't change but the textbox goes blank everytime I hit the button...
Daok
Oh! Editted answer, you need OneWayToSource binding. OneWay does the opposite of what you intend.
Will Eddins
Which i'm probably wrong again. The resource I was reading confused me (on which object is considered the 'source') but I believe you were doing it correctly before. It almost seems like overkill at this point where the problem could be solved instead with a this.Url = this.txtUrl.Text but it wouldn't solve the issue of why this is happening.
Will Eddins
+2  A: 

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

Daok
+1 since it's interesting that the explicit binding updates allow you to specify the direction of the update on the fly. I suppose a simple "update" in a TwoWay case where both the source and target changed would have unknown results. Good stuff to know.
Will Eddins