Wpf TextBox BindingExpression UpdateSourceTrigger=显式

Wpf TextBox BindingExpression UpdateSourceTrigger=显式,wpf,listview,binding,binding-expressions,Wpf,Listview,Binding,Binding Expressions,ListView数据模板中需要一个文本框,以便在LostFocus或enter键上调用set。Used UpdateSourceTrigger=用于LostFocus和KeyUp的显式和事件。问题是我无法获得对BindingExpression的有效引用 XAML be是空的。我尝试了ListView.SelectedValueProperty和ListView.SelectedPathProperty。如果它尝试tbExistingValue,则失败,消息不存在,甚至不会编译。如何获得正确的B

ListView数据模板中需要一个文本框,以便在LostFocus或enter键上调用set。Used UpdateSourceTrigger=用于LostFocus和KeyUp的显式和事件。问题是我无法获得对BindingExpression的有效引用

XAML

be是空的。我尝试了ListView.SelectedValueProperty和ListView.SelectedPathProperty。如果它尝试tbExistingValue,则失败,消息不存在,甚至不会编译。如何获得正确的BindingExpression??谢谢

如果我设置UpdateSourceTrigger=LostFocus并删除事件处理程序,它会正确调用set。这里有一个有效的双向绑定。我只是无法使用显式获取对BindingExpression的有效引用

它适用于网格单元格中直接位于页面上的文本框。下面的xaml可以工作:

    <TextBox Grid.Row="1" Grid.Column="1" x:Name="strAddRow" 
             Text="{Binding Path=DF.NewFieldValue, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}"
             Validation.Error="Validataion_Error" 
             LostFocus="strAddRow_LostFocus" KeyUp="strAddRow_KeyUp"/>

由于您在Textbox的文本DP上应用了绑定,因此您只需要像这样从那里获取绑定-

private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
   BindingExpression be = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
   be.UpdateSource();
}

此外,还没有将ListView SelectedItem与ViewModel的任何属性绑定。要检索绑定,它至少应该绑定到某个值。因此,您应该将其绑定到FieldValueProperty,这样您的代码就不会得到空值。

因为您在文本框的文本DP上应用了绑定,所以您只需要像这样从那里获取绑定-

private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
   BindingExpression be = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
   be.UpdateSource();
}

此外,还没有将ListView SelectedItem与ViewModel的任何属性绑定。要检索绑定,它至少应该绑定到某个值。因此,您应该将其绑定到FieldValueProperty,这样您的代码就不会得到空值。

您不需要使用事件LostFocus在文本框上使用UpdateSourceTrigger。 这是默认的功能


在此处回答:

您不需要使用事件LostFocus在文本框上使用UpdateSourceTrigger。 这是默认的功能


回答这里:

这很有效,谢谢。而且它不需要绑定ListView SelectedItem即可工作。谢谢。并且它不绑定ListView SelectedItem。
    private void strAddRow_LostFocus(object sender, RoutedEventArgs e)
    {
        BindingExpression be = strAddRow.GetBindingExpression(TextBox.TextProperty);
        be.UpdateSource();
    }
private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
   BindingExpression be = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
   be.UpdateSource();
}