Validating a DataGrid Column (WPF)

I would like to know how to programmatically run a check on a DataGridColumn. It would be pretty much the same as donde calling the UpdateSource method for the BindingExpression, but I am unable to get the BindingExpression of the column.

Thanks.

PS: setting the ValidatesOnTargetUpdated property to a ValidationRule is not what I'm looking for :)

+7
c # data-binding wpf datagrid
source share
3 answers

In the .NET Framework 4, a namespace called System.ComponentModel.DataAnnotations is available for both the regular CLR (WPF) and the lighter Silverlight CLR. You can use the DataAnnotations namespace for various purposes. One of them is checking data using attributes, and the other is a visual description of fields, properties and methods, or setting the data type for a particular property. These three categories are classified in the .NET Framework as validation attributes, display attributes, and data modeling attributes. This section uses validation attributes to define validation rules for objects.

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

+1
source share

@ user424096,

I do not have access to my visual studio environment, but after pseudo-code you can find the way you need ...

  • Create a related logical property NotifySourceUpdates and attach it to the DataGridCell ... I have bound it at the datagrid level so that it applies to all cells of the data grid ... you can also attach it at the column level ...

    <DataGrid ItemsSource="{Binding}"> <DataGrid.CellStyle> <Style TargetType="DataGridCell" > <Setter Property="ns:MyAttachedBehavior.NotifySourceUpdates" Value="True"/> </Style> </DataGrid.CellStyle> </DataGrid> 
  • This attached behavior will handle an attached event called Binding.SourceUpdated at the cell level. Therefore, whenever a binding as part of any user interface element of a child element or editing mode is updated, it will fire and bubble at the cell level.

      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); } } 
  • In this event handler, the event arguments are of type DataTransferEventArgs, which gives you a TargetObject. This will be your control that needs to be checked.

     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. } } 
  • Here you should know which value represented by the control is valid or invalid.

     (uiElement.MyValue == null) //// Invalid!! 
  • If you want the control binding to be invalid, just use the MarkInvalid call using the following steps ...

     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 ...

+1
source share

You might consider using System.ComponentModel.IDataErrorInfo to provide verification of such inputs.

0
source share

All Articles