使用访问键时 LostFocus 绑定不起作用

发布于 2024-07-09 11:40:40 字数 1659 浏览 10 评论 0原文

请考虑以下示例。 请注意,实际上,绑定源可能是数据对象。 为简单起见,我使用 TextBlock

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Margin="20">

        <Label>Enter a Name:</Label>
        <TextBox x:Name="txt_Name" Text="{Binding ElementName=display_name, Path=Text, UpdateSourceTrigger=LostFocus}" />

        <Label>The name you entered:</Label>
        <TextBlock x:Name="display_name" />

        <Button x:Name="btn_Save" Click="SaveClick">_Save</Button>

    </StackPanel>
</Window>
Class Window1

    Private Sub SaveClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MessageBox.Show("Saving your name as: " & display_name.Text)
    End Sub

End Class

在上面的示例中,如果我在 TextBox 中输入名称 "Joe" 并单击“保存”按钮,则 TextBlock 会更新LostFocus 并且数据已正确“保存”。 一切都很好。

但是,如果我随后在 TextBox 中输入 "Bob" 并使用访问键 (Alt-S) 进行保存,则 TextBlock 不会已更新,因为 TextBox 上的 LostFocus 事件未触发。 因此,我的绑定源未更新,并且保存了错误的值(即 "Joe")。

在大多数 WPF TextBox 数据输入字段中,您需要在 LostFocus(而不是 PropertyChanged)上进行验证; 但是,如果使用访问密钥时 LostFocus 事件未触发(因此绑定未更新),我们如何验证该条目? 在 WinForms 中,我们有 ValidatingValidated 事件,但它们在 WPF 中不存在。

Please consider the following example. Note that in the real-word, the binding source will likely be a data object. I'm using a TextBlock for simplicity.

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Margin="20">

        <Label>Enter a Name:</Label>
        <TextBox x:Name="txt_Name" Text="{Binding ElementName=display_name, Path=Text, UpdateSourceTrigger=LostFocus}" />

        <Label>The name you entered:</Label>
        <TextBlock x:Name="display_name" />

        <Button x:Name="btn_Save" Click="SaveClick">_Save</Button>

    </StackPanel>
</Window>
Class Window1

    Private Sub SaveClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        MessageBox.Show("Saving your name as: " & display_name.Text)
    End Sub

End Class

In the example above, if I enter the name "Joe" into the TextBox and click the "Save" button, the TextBlock is updated upon LostFocus and the data is "saved" properly. All is good.

However, if I then enter "Bob" in the TextBox and use my access key (Alt-S) to save, the TextBlock is not updated because the LostFocus event on the TextBox is not fired. As a result, my binding source is not updated and the wrong value (i.e., "Joe") is saved.

In most WPF TextBox data entry fields, you'll want to do your validation upon LostFocus (not PropertyChanged); however, if the LostFocus event does not fire (and thus the binding is not updated) when an access key is used, how do we validate the entry? In WinForms we have the Validating and Validated events, but they are absent in WPF.

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

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

发布评论

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

评论(3

美男兮 2024-07-16 11:40:40

您可以在保存之前手动执行此操作:

txt_Name.GetBindingExpression(TextBox.TextProperty).UpdateTarget();

有点难看,但它有效。

You can do this manually just before the save using:

txt_Name.GetBindingExpression(TextBox.TextProperty).UpdateTarget();

A bit ugly, but it works.

↙温凉少女 2024-07-16 11:40:40

您还可以在读取值之前更改单击处理程序中的焦点,例如将焦点强制到按钮或另一个文本框

这是另一个“丑陋但有效”的解决方案,如果您有很多控件,它可能是合适的或者不想弄乱它们的绑定表达式。

You can also change focus in your click handler before reading the value, for example forcing the focus to the button or to another text box

This is another "ugly, but it works" solution, it may be appropriate if you have a lot of controls or don't want to mess with their binding expressions.

眼眸里的快感 2024-07-16 11:40:40

好吧,如果您对现实世界的场景比此处人为的示例更感兴趣,则可以将文本框上的绑定设置为更新数据更改而不是失去焦点,并且数据将在两种场景中保存,而不会出现任何丑陋的情况黑客。 唯一的问题(在绑定的 WPF 文档中提到)是这可能会影响应用程序的性能。 如果您在任何类型的甚至相对较新的机器上运行,您将不会注意到其中的差异(我没有)。

Well, if you're more interested in your real world scenario than your contrived example here, you can set the binding on the text box to update on data change rather than lost focus, and the data will be saved in both scenarios without any ugly hacks. The only problem (which is mentioned in the WPF documentation for Bindings) is that this may impact the performance of your application. If you're running on any sort of even relatively new machine, you won't notice the difference ( I haven't ).

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