WPF:MVVM - 在 ivalueconverter 转换异常上禁用命令附加按钮

发布于 2024-10-20 19:04:45 字数 1026 浏览 3 评论 0原文

我有一个简单的场景和一个问题,过去几天我似乎无法解决。

好的,首先我使用 MVVM 将我的视图绑定到 ViewModel 上。我认为有几个文本框绑定到 ViewModel 中的多个属性(大多数字符串)(实际上绑定到带有字符串属性的自定义对象(类型为 Person,名称为 SelectedPerson),该对象是视图模型的属性)。该对象实现 INotifyPropertyChanged 和 IDataErrorInfo。它还有一个名为 Age 的 int 属性。我的视图中还有一个按钮,它绑定到我的视图模型中的命令,该命令在 CanExecute 中测试 SelectedPerson 的属性,如果所有属性都正确,则返回 true。

现在我的问题是:如果我从我的视图中输入年龄文本框,而不是int,则会出现一个红色的矩形(这是正常的,因为转换有一个例外),但在那个特定的时刻,到后面的对象( SelectedPerson,类型 Person)没有发送 newValue (该属性 Age 的设置器,或者 IDataErrorInfo 成员不会拦截该值......我想这是正常的,因为没有任何“新”值,因为我首先在文本框中输入了错误的格式)。

所以,也许我重复一遍,问题是:如果没有设置新的年龄(新的不正确的年龄),那么后面的对象仍然保留最后的值,如果它是正确的,那么命令本身就是正确的(可以执行将返回 true)并且该按钮启用

正如您可以想象的那样,我希望当当前属性未通过转换时禁用提交按钮(这是一个在数据存储模块中保存当前人员详细信息的按钮)方法。

PS:我使用了 IValueConverter 类,在绑定到 Age 的文本框中,我使用了 StringToIntConverter 类....但在 Convert 方法上我不知道如何传递 SelectedPerson 绑定对象(我只是传递文本值,并返回 int 值)

我想一种方法可以是使用 MultiBinding 方案,但我不确定。

如果我可以从该转换器传递 Convert 方法中的 SelectedPerson,我就可以使转换器本身的该命令无效。

抱歉我的英语不好,我知道它远非完美:),提前感谢您的宝贵时间。

I have a simple scenario and a issue with it which I just cant seem to resolve past few days.

OK, first of all I use MVVM to bind my View on a ViewModel. I have in my view several text boxes which binds to several properties (most strings) in ViewModel (binds actually to an Custom Object (type Person, name SelectedPerson) with strings properties , object which is a property of the viewmodel). This object implements INotifyPropertyChanged and IDataErrorInfo. It has also an int property named Age. I also have in my view a button which is bound to a command in my viewmodel, a command which inside CanExecute test the SelectedPerson's properties and return true if all are correct.

Now my issue is: if I put in my Age text box from my View something not int, a red tectagle will appear (is normal, because there is an exception to the conversion), but in that specific moment, to the object behind (SelectedPerson, type Person) there isn't sent the newValue (the setter to that property Age, or the IDataErrorInfo Members don't intercept the value .... I guess it is normal because there isn't any "new" value, because I put an incorrect format in the text box in the first place).

So, maybe I repeat myself, the issue is: if the new Age (new incorrect Age) isn't set, then the Object behind still hold last value, which if it was correct then the command itself it's correct (the can execute will return true) and the button is enabled

As you can imagine I want the submit button (it's a button which saves current person details in data storage module) to be disabled when current properties don't pass through conversions methods.

PS: I used a IValueConverter class , and on that text box binded to Age, I made use of my StringToIntConverter class....but on Convert Method I don't know how to pass the SelectedPerson binded object (I just pass the text value, and return the int value)

I guess one way to do it could be by using MultiBinding scenario , but I'm not sure.

If I could pass the SelectedPerson inside Convert method from that converter I could invalidate that command from the converter itself.

Sorry for my English, I know it's far from perfect :) and thanks in advance for your time.

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

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

发布评论

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

评论(1

最冷一天 2024-10-27 19:04:45

我认为最干净的解决方案是将文本框绑定到字符串属性,并更新视图模型,以便该属性名称的 IDataErrorInfo 实现尝试将字符串转换为 int 并返回结果。然后,您的 CanExecute 测试还将包含此转换作为验证测试​​的一部分。

I think the cleanest solution would be to bind the textbox to a string property instead, and update your view model so that your IDataErrorInfo implementation for that property name attempts a string to int conversion and returns the result of that. Your CanExecute test would also then include this conversion as part of the validation test.

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