首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TextBox BindingExpression UpdateSourceTrigger=Explicit

TextBox BindingExpression UpdateSourceTrigger=Explicit
EN

Stack Overflow用户
提问于 2011-10-16 21:39:29
回答 2查看 4.6K关注 0票数 2

需要ListView DataTemplate中的TextBox才能在LostFocus或enter键上调用set。对LostFocus和KeyUp使用UpdateSourceTrigger = Explicit and events。问题是我无法获得对BindingExpression的有效引用。

XAML

代码语言:javascript
复制
    <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>

代码隐藏不起作用

代码语言:javascript
复制
    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是有效的:

代码语言:javascript
复制
    <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运行得很好:

代码语言:javascript
复制
    private void strAddRow_LostFocus(object sender, RoutedEventArgs e)
    {
        BindingExpression be = strAddRow.GetBindingExpression(TextBox.TextProperty);
        be.UpdateSource();
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-16 22:28:55

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

代码语言:javascript
复制
private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
{
   BindingExpression be = (sender as TextBox).GetBindingExpression(TextBox.TextProperty);
   be.UpdateSource();
}

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

票数 2
EN

Stack Overflow用户

发布于 2015-11-19 19:41:26

您不需要在使用event LostFocus的TextBox上使用UpdateSourceTrigger。这是默认的功能。

答案在这里:https://msdn.microsoft.com/en-gb/library/system.windows.data.binding.updatesourcetrigger(v=vs.110).aspx

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7784625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档