TextBox BindingExpression UpdateSourceTrigger=显式

发布于 2024-12-09 20:14:38 字数 2543 浏览 0 评论 0原文

ListView DataTemplate 中需要一个文本框来调用 LostFocus 或 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();
    } 

为 null。我尝试过 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();
    }

Need a TextBox in a ListView DataTemplate to call set on either LostFocus or enter key. Used UpdateSourceTrigger = Explicit and events for LostFocus and KeyUp. Problem is that I cannot get a valid reference to the 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>

Code Behind NOT working

    private void tbExistingValue_LostFocus(object sender, RoutedEventArgs e)
    {
        BindingExpression be = lvMVitems.GetBindingExpression(ListView.SelectedItemProperty);
        be.UpdateSource();
    } 

be is null. I have tried ListView.SelectedValueProperty and ListView.SelectedPathProperty. If it try tbExistingValue it fails with a message "does not exists" and will not even compile. How do I get the proper BindingExpression?? Thanks.

If I set UpdateSourceTrigger = LostFocus and remove the event handlers it does call set properly. There is a valid twoway binding there. I just cannot get a valid reference to BindingExpression (be) using explicit.

It works fine for a TextBox directly on page (in a grid cell). The xaml below works:

    <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"/>

This BindingExpression works fine:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山川志 2024-12-16 20:14:38

由于您将绑定应用于 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,这样您的代码就不会获得空值。

Since you apply the binding on your Textbox's Text DP, so you need to fetch the binding from there only like this -

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

Moreover, you haven't bind the ListView SelectedItem with any property of your ViewModel. To retrieve the binding, it should be atleast binded to some value. So, you should bind it to your FieldValueProperty then you won't get null value with your code in place.

伴我老 2024-12-16 20:14:38

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

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

You do not need to use UpdateSourceTrigger on TextBox using the event LostFocus.
It's the functioning by default.

Answer here : https://msdn.microsoft.com/en-gb/library/system.windows.data.binding.updatesourcetrigger(v=vs.110).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文