简体   繁体   English

强制DataGrid列验证(WPF)

[英]Force DataGrid column validation (WPF)

I would like to know how to programmatically fire validation over a DataGridColumn. 我想知道如何以编程方式在DataGridColumn上激活验证。 It would be pretty much the same as it is donde calling the UpdateSource method of a BindingExpression, but I cant manage to get the BindingExpression of the column. 它和donde调用BindingExpression的UpdateSource方法几乎相同,但是我无法获得列的BindingExpression。

Thanks. 谢谢。

PS: setting the ValidatesOnTargetUpdated property on the ValidationRule is not what I'm looking for :) PS:在ValidationRule上设置ValidatesOnTargetUpdated属性不是我想要的:)

In the .NET Framework 4, a namespace called System.ComponentModel.DataAnnotations is available for both the common CLR (WPF) and the lighter Silverlight CLR. 在.NET Framework 4中,名为System.ComponentModel.DataAnnotations的命名空间可用于公共CLR(WPF)和较轻的Silverlight CLR。 You can use the DataAnnotations namespace for various purposes. 您可以将DataAnnotations命名空间用于各种目的。 One of these is for data validation using attributes, and another is the visual description of fields, properties, and methods, or to customize the data type of a specific property. 其中一个是使用属性进行数据验证,另一个是字段,属性和方法的可视化描述,或自定义特定属性的数据类型。 These three categories are classified in the .NET Framework as Validation Attributes, Display Attributes, and Data Modeling Attributes. 这三个类别在.NET Framework中分类为验证属性,显示属性和数据建模属性。 This section uses Validation Attributes to define validation rules for objects 本节使用验证属性来定义对象的验证规则

http://www.codeproject.com/KB/dotnet/ValidationDotnetFramework.aspx http://www.codeproject.com/KB/dotnet/ValidationDotnetFramework.aspx

@user424096, @ user424096,

I have no access to my visual studio environment, but following pseudo code may guide you for your desired way... 我无法访问我的视觉工作室环境,但遵循伪代码可能会指导您按照您想要的方式...

  1. Create an attached boolean property called NotifySourceUpdates and attach is to DataGridCell... I have attached it at datagrid level so that it applies to all data grid cells... you can attach it at column level as well... 创建一个名为NotifySourceUpdates的附加布尔属性,并将其附加到DataGridCell ...我已将其附加到datagrid级别,以便它适用于所有数据网格单元...您也可以在列级别附加它...

      <DataGrid ItemsSource="{Binding}"> <DataGrid.CellStyle> <Style TargetType="DataGridCell" > <Setter Property="ns:MyAttachedBehavior.NotifySourceUpdates" Value="True"/> </Style> </DataGrid.CellStyle> </DataGrid> 
  2. This attached behavior will handle the attached event called Binding.SourceUpdated at the cell level. 此附加行为将处理在单元级别称为Binding.SourceUpdated的附加事件。 So whenever any binding as part of any child UI element's normal or edit mode has its source updated, it will fire and bubble to the cell level. 因此,每当任何绑定作为任何子UI元素的正常或编辑模式的一部分更新其源时,它将触发并冒泡到单元级别。

      public static readonly DependencyProperty NotifySourceUpdatesProperty = DependencyProperty.RegisterAttached( "NotifySourceUpdates", typeof(bool), typeof(MyAttachedBehavior), new FrameworkPropertyMetadata(false, OnNotifySourceUpdates) ); public static void SetNotifySourceUpdates(UIElement element, bool value) { element.SetValue(NotifySourceUpdatesProperty, value); } public static Boolean GetNotifySourceUpdates(UIElement element) { return (bool)element.GetValue(NotifySourceUpdatesProperty); } private static void OnNotifySourceUpdates(DependencyObject d, DependencyPropertyEventArgs e) { if ((bool)e.NewValue) { ((DataGridCell)d).AddHandler(Binding.SourceUpdated, OnSourceUpdatedHandler); } } 
  3. In this event handler, the event args are of type DataTransferEventArgs which gives you the TargetObject. 在此事件处理程序中,事件args的类型为DataTransferEventArgs,它为您提供TargetObject。 This will be your control that needs to validate. 这将是您需要验证的控件。

     private static void OnSourceUpdatedHandler(object obj, DataTransferEventArgs e) //// Please double check this signature { var uiElement = e.TargetObject as UIElement; if (uiElement != null) { ///... your code to validated uiElement. } } 
  4. Here you must know what value represented by the control is valid or invalid. 在这里,您必须知道控件表示的值是有效还是无效。

     (uiElement.MyValue == null) //// Invalid!! 
  5. If you want the control's binding to invalidate, just use the MarkInvalid call using these steps... 如果您希望控件的绑定无效,只需使用这些步骤使用MarkInvalid调用...

     ValidationError validationError = new ValidationError(myValidationRule, uiElement.GetBindingExpression(UIElement.MyValueDependecyProperty)); validationError.ErrorContent = "Value is empty!"; Validation.MarkInvalid(uiElement.GetBindingExpression(UIElement.MyValueDependencyProperty), validationError); 

Let me know if this works... 让我知道这个是否奏效...

您可以考虑实现System.ComponentModel.IDataErrorInfo来为这些输入提供验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM