按下按钮时绑定(显式绑定)总是BindingExpression为null?

Pat*_*ins 2 .net c# wpf binding

我有一个包含字符串的文本框,只有当用户按下按钮时才必须绑定该字符串.在XAML中:

<Button Command="{Binding Path=PingCommand}" Click="Button_Click">Go</Button>
<TextBox x:Name="txtUrl" Text="{Binding Path=Url,UpdateSourceTrigger=Explicit, Mode=OneWay}" />
Run Code Online (Sandbox Code Playgroud)

在代码隐藏中:

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

"be"始终为NULL.为什么?

更新:

好的,这是经过大量尝试后的一些更新.

如果,我将模式OneWay设置为显式更新.我在GetBindingExpression的"be"对象中有一个NullReferenceException.

如果我使用显式更新将模式设置为空(默认为TwoWay).我有绑定获取值(string.empty)并且它每次都会删除文本框中的所有内容.

如果我将Mode设置为OneWay,使用PropertyChanged当我按下文本框中的键时,绑定的属性没有任何内容,当我单击按钮时,我在"be"对象中有一个NULLReferenceException.

如果我将模式设置为空(默认为TwoWay),使用PropertyChanged我每次按(GOOD)时都会提高属性但我不希望每次用户按键时都更改属性...但只有一次用户按下按钮.

Pat*_*ins 7

好吧,经过多思考后,我注意到:

 BindingExpression be = this.txtUrl.GetBindingExpression(TextBox.TextProperty);
 be.UpdateTarget();
Run Code Online (Sandbox Code Playgroud)

有一些不合逻辑的东西,因为我不想更新目标而是来源.我只是将be.Updatetarget()改为be.UpdateSource(); 一切都适用于这个XAML:

 <TextBox x:Name="txtUrl" Text="{Binding Path=Url, UpdateSourceTrigger=Explicit}">...
Run Code Online (Sandbox Code Playgroud)

感谢所有帮助我解决这个问题的人.我给每个人加了+1!谢谢