DataGrid validation with disabling "Save" button when there are any validation errors.

May 19, 2010 at 12:23 PM

Hi there! I'm searching for complete example of implementing validation of specific columns' data, but I can't find complete working example, and almost all examples use various ways of validating( What way is best?

What I need:

I have form, where I have some button. When I click button, it creates user control (it consists of datagrid and couple of buttons - save and reload) and put it on form.

1) I need my DataGrid to validate some of its colums with custom rules

2) Change template of row and cell when cell in error state

3) Show custom tooltip with error message for every single cell, which is in error state.

4) When there are validation errors in my user control, disable save button

this is my user control xaml:

<UserControl x:Class="Client.Managers"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"			
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="RootGrid1">
		<Grid.RowDefinitions>
			<RowDefinition Height="*" />
			<RowDefinition Height="29" />
		</Grid.RowDefinitions>
		<DataGrid IsSynchronizedWithCurrentItem="True"
				  ItemsSource="{Binding Path=Managers}"
				  AutoGenerateColumns="False"				  
				  HorizontalAlignment="Left"				  
				  Name="dataGrid1"
				  VerticalAlignment="Top">
			<DataGrid.RowValidationRules>
                          <DataErrorValidationRule />
			</DataGrid.RowValidationRules>
			<DataGrid.Columns>
				<DataGridTextColumn Header="ID" Binding="{Binding Path=ManagerID}" />
				<DataGridTextColumn Header="Name" x:Name="ValidCell">
					<DataGridTextColumn.Binding>
						<Binding Path="Name" ValidatesOnDataErrors="False" ValidatesOnExceptions="False">
							<Binding.ValidationRules>				<DataErrorValidationRule />		</Binding.ValidationRules>
						</Binding>
					</DataGridTextColumn.Binding>
				</DataGridTextColumn>
				<DataGridTextColumn Header="Rate">
					<DataGridTextColumn.Binding>
						<Binding Path="Rate">
							<Binding.ValidationRules>
								<DataErrorValidationRule />
							</Binding.ValidationRules>
						</Binding>
					</DataGridTextColumn.Binding>
				</DataGridTextColumn>
				<DataGridTextColumn Header="Created"
											Binding="{Binding Path=Created}" />
				<DataGridTextColumn Header="By"
											Binding="{Binding Path=Created_by}" />
				<DataGridTextColumn Header="Modified"
											Binding="{Binding Path=Modified}" />
				<DataGridTextColumn Header="By"
											Binding="{Binding Path=Modified_by}" />
			</DataGrid.Columns>
		</DataGrid>
		<Button Content="Button"
				Grid.Row="1"
				Height="23"
				HorizontalAlignment="Left"
				Name="button1"
				VerticalAlignment="Top"
				Width="75"
				Click="button1_Click" />
		<Button Content="Button"
				Grid.Row="1"
				Height="23"
				HorizontalAlignment="Left"
				Margin="89,0,0,0"
				Name="button2"
				VerticalAlignment="Top"
				Width="75"
				Click="button2_Click" />
	</Grid>
</UserControl>

 

And this is code behind:

using System.Windows;
using System.Windows.Controls;
using DAL;

namespace Client
{
	/// <summary>
	/// Interaction logic for Managers.xaml
	/// </summary>
	/// 
	public partial class Managers : UserControl
	{
		ManagersModel managersmodel = new ManagersModel();
		public Managers()
		{
			InitializeComponent();			
			dataGrid1.DataContext = managersmodel;
		}

		private void button1_Click( object sender, RoutedEventArgs e )
		{
			managersmodel.SaveChanges();
		}

		private void button2_Click( object sender, RoutedEventArgs e )
		{
			managersmodel = new ManagersModel();
			dataGrid1.DataContext = managersmodel;
		}
	}	
}
So, it is very interesting for me, how to disable button, when validation error exists and enable when it dissapear. Even maybe without extra validation rules, at least with DataErrorValidationRule