gpt4 book ai didi

wpf - MVVM - WPF : Updating view model boolean property when TextBox is being edited

转载 作者:行者123 更新时间:2023-12-03 10:28:44 32 4
gpt4 key购买 nike

我找到了一些关于如何从 View 模型中选择控件的示例,但不能以正确的术语搜索如何让它以相反的方式工作。基本上,我只想在我的 View 模型中更新一个 bool 值,当一个文本框被编辑时,什么时候没有。我认为这与想知道它何时获得和失去焦点是一样的。

所以我创建了一个 FocusExtension 类,如下所述:Set focus on textbox in WPF from view model (C#)

但它不会在我的 XAML 中使用以下行触发对我的 View 模型属性“EditingMyTargetField”的访问器:

my:FocusExtension.IsFocused="{Binding EditingMyTargetField}"

最佳答案

我会探索除最佳答案之外的其他答案,因为其他人已经改进了批准的解决方案。我用一个文本框测试了 Zamotic 的 FocusExtension,它成功地触发了与 View 模型的绑定(bind)。他的解决方案具有在元素获得或失去焦点时触发的事件,然后将 dependencyproperty 值设置为正确的状态。我还必须在标记中明确将绑定(bind)模式声明为 TwoWay,但如果需要,您可以更改扩展名以默认设置双向绑定(bind)。

<TextBox local:FocusExtension.IsFocused="{Binding TextIsFocused, Mode=TwoWay}"/>

关于wpf - MVVM - WPF : Updating view model boolean property when TextBox is being edited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848129/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com