【问题标题】:TextBox BindingExpression UpdateSourceTrigger=ExplicitTextBox BindingExpression UpdateSourceTrigger=Explicit
【发布时间】:2011-12-08 17:39:55
【问题描述】:

需要 ListView DataTemplate 中的 TextBox 来调用 LostFocus 上的 set 或 enter 键。使用 UpdateSourceTrigger = Explicit 和 LostFocus 和 KeyUp 的事件。问题是我无法获得对 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 正确的事件处理程序。那里有一个有效的双向绑定。我只是无法使用显式获得对 BindingExpression (be) 的有效引用。

它适用于直接在页面上的文本框(在网格单元中)。下面的 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();
    }

【问题讨论】:

    标签: wpf listview binding binding-expressions


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。它可以在不绑定 ListView SelectedItem 的情况下工作。
    【解决方案2】:

    您不需要使用事件 LostFocus 对 TextBox 使用 UpdateSourceTrigger。 这是默认的功能。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2014-04-19
      相关资源
      最近更新 更多