模板列中带有文本框的 WPF DataGrid 未触发属性更改

发布于 2024-11-15 12:57:50 字数 1196 浏览 3 评论 0原文

我有一个带有模板列的数据网格,该模板中有一个文本框。它绑定到组成 itemssource 的集合中对象的“Quantity”属性。数量是一个整数。当我将一个项目添加到数据网格时,我向该项目的 PropertyChanged 事件添加一个事件处理程序:

EnteredPart.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(p_PropertyChanged);

当用户在文本框中输入整数时,这可以正常工作... p_PropertyChanged 处理程序触发,我可以禁用按钮并更改数量如有必要。当用户输入非整数时,处理程序不会被调用,我只是在文本框周围看到一个红色轮廓。因此,我无法在需要时禁用必要的按钮(它们应该因数量不合法而被禁用。)有什么办法可以解决这个问题吗?

编辑:我尝试将 Quantity 属性更改为字符串,这导致在输入非整数值时调用属性更改处理程序。不过,我随后向文本框添加了验证来检查这一点,如果 Validate 方法返回 false,则属性更改处理程序将再次停止被命中。有没有办法同时获得验证和属性更改通知?

编辑2:这是我在另一个位置遇到的这个问题的另一个实例。我有一个用于添加/编辑电话号码的表格。电话号码文本框如下所示:

  <TextBox >
       <TextBox.Text>
           <Binding Path="Phone.Number">
               <Binding.ValidationRules>
                   <local:PhoneValidationRule />
               </Binding.ValidationRules>
           </Binding>
       </TextBox.Text>
   </TextBox>

单击保存按钮后,我需要检查我的视图模型该号码是否有效,因为如果无效,我不想运行保存命令。但是,似乎没有任何方法可以做到这一点,因为如果验证失败,则 Phone.Number 属性具有空值,并且我无法检查是否应该运行保存命令。我要么需要访问错误状态(我认为可以通过 Validation.GetErrors 工作,但事实并非如此),要么需要访问文本框中的实际文本,这在视图模型中不可用。

I have a datagrid with a template column, and in that template is a textbox. It's bound to the 'Quantity' property of the object in the collection that makes up itemssource. Quantity is an integer. When I add an item to the datagrid, I am adding an event handler to the PropertyChanged event of the item:

EnteredPart.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(p_PropertyChanged);

This works fine when the user enters an integer in the textbox... the p_PropertyChanged handler fires and I can disable buttons and alter quantities as necessary. When the user enters a non-integer, the handler doesn't get called, I just get a red outline around the textbox. Because of this, I can't disable the necessary buttons when I need to (they should be disabled with the quantity is not legit.) Is there any way that I can do something about this?

EDIT: I tried changing the Quantity property to a string, and this caused the property changed handler to be called when non-integral values are entered. However, I then added validation to the textbox to check for this, and if the Validate method returns false, the property changed handler once again ceases to be hit. Is there any way at all to get both validation and property changed notifications??

EDIT 2: Here another instance of this problem I'm having, in another location. I have a form for adding/editing phone numbers. The phone number textbox looks like this:

  <TextBox >
       <TextBox.Text>
           <Binding Path="Phone.Number">
               <Binding.ValidationRules>
                   <local:PhoneValidationRule />
               </Binding.ValidationRules>
           </Binding>
       </TextBox.Text>
   </TextBox>

Upon clicking a save button, I need to check in my viewmodel if the number is valid, because if it's not, I don't want to run the save command. However, it doesn't seem as if there's any way to do that, because if the validation fails, then the Phone.Number property has a null value, and I have no way to check to see if I should run the save command. I either need access to the error state (which I thought would work by Validation.GetErrors, but doesn't), or to the actual text within the textbox, which isn't available in the viewmodel.

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

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

发布评论

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

评论(1

旧情别恋 2024-11-22 12:57:50

仅供记录,如果您处于 MVVM 架构中,视图部分中的验证有很多缺点。

您可以尝试在视图中检查模型的内容:因此,您可以通过在视图中调用模型来破坏 MVVM 的架构。

使用 IDataErrorInfo 将帮助您实现 MVVM 的主要目标(即清楚地区分三个部分)。

只是一个例子:

我认为这里您只是执行一个非常小的验证(只需检查它是否是 int )。
但在不同的环境中,假设您的模型要复杂得多,并且需要更深入的验证。使用 IDataErrorInfo 将帮助您深入检查模型,而无需从视图中调用它。
事实上,根据我个人的经验,由于我经常处理大型且高度相关的数据集,我什至无法想象在没有 IDataErrorInfo 的情况下使用验证,因为调查所有这些数据会花费我太多的钱。提供数据并发现潜在错误

Just for the record, validation in the View part has many drawbacks if you are in an MVVM architecture.

You'd try here to check your model's content, in the view: you'd therefore break MVVM's architecture by calling your model in the view.

Using IDataErrorInfo will help you to fulfill MVVM's main objective (ie. clearly separate the three parts).

Just an example:

I think here you are just performing a very small validation (just check if it is an int or not).
But in a different environment, assume that your model is much more complicated and needs a deeper validation. Using IDataErrorInfo will help you to check deeply in your model without calling it from the view.
As a matter of fact, in my personal experience, as I regularly work with large and highly correlated datasets, I cannot even picture using validation without IDataErrorInfo because it'd cost me too much to investigate in all the data presented and find potential errors

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