需要ListView DataTemplate中的TextBox才能在LostFocus或enter键上调用set。对LostFocus和KeyUp使用UpdateSourceTrigger = Explicit and events。问题是我无法获得对BindingExpression的有效引用。
XAML
<ListView x:Name="lvMVitems" ItemsSource="{Binding Path=DF.DocFieldStringMVitemValues, Mode=OneWay}">
<ListView.View>
<GridView>
<GridViewColumn x:Name="gvcExistingValue">
<GridViewColumnHeader Content="Value"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="tbExistingValue"
Text="{Binding Path=FieldItemValue, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=Explicit}"
Validation.Error="Validataion_Error"
LostFocus="tbExistingValue_LostFocus" KeyUp="tbExistingValue_KeyUp" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>代码隐藏不起作用
private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
BindingExpression be = lvMVitems.GetBindingExpression(ListView.SelectedItemProperty);
be.UpdateSource();
} be为空。我已经尝试过ListView.SelectedValueProperty和ListView.SelectedPathProperty。如果它尝试tbExistingValue,它会失败,并显示一条消息“不存在”,甚至不会编译。如何获得正确的BindingExpression??谢谢。
如果我设置UpdateSourceTrigger = LostFocus并删除事件处理程序,它将正确调用set。这里有一个有效的双向绑定。我无法使用explicit获得对BindingExpression (be)的有效引用。
它适用于直接在页面上(在网格单元格中)显示的TextBox。下面的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"/>这个BindingExpression运行得很好:
private void strAddRow_LostFocus(object sender, RoutedEventArgs e)
{
BindingExpression be = strAddRow.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}发布于 2011-10-16 22:28:55
由于您在Textbox的文本DP上应用了绑定,因此您只需要像这样从那里获取绑定-
private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
BindingExpression be = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();
}此外,您还没有将ListView SelectedItem与ViewModel的任何属性绑定。要检索绑定,至少应该将其绑定到某个值。所以,你应该把它绑定到你的FieldValueProperty上,这样你的代码就不会得到空值了。
发布于 2015-11-19 19:41:26
您不需要在使用event LostFocus的TextBox上使用UpdateSourceTrigger。这是默认的功能。
https://stackoverflow.com/questions/7784625
复制相似问题