简体   繁体   中英

Validation rules for entries of a particular row in a WPF DataGrid

I have a DataGrid (MyDgr) with 2 columns (Column1 and Column2) and 2 rows. I am able to set Validation rules for my DataGrid by Columns. Ex: For Column1, all entries must be positive, and for Column2, all entries must be <5.

However, I would also like to be able to set different Validation Rules for each row. For instance, entries in the first row can only be integers, while entries in the second row are allowed to be doubles.

The way I am setting 'Column-Validation Rules', I have created 2 classes for the 2 columns

public class Column1ValidationRule : ValidationRule
{

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        if (value != null)
        {
            double proposedValue;
            if (!double.TryParse(value.ToString(), out proposedValue))
            {
                return new ValidationResult(false, "'" + value.ToString() + "' is not a real number.");
            }

            if (proposedValue <= 0)
            {
                return new ValidationResult(false, "value must be positive.");
            }
        }

        return new ValidationResult(true, null);
    }
}

public class Column2ValidationRule : ValidationRule
{

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        if (value != null)
        {
            double proposedValue;
            if (!double.TryParse(value.ToString(), out proposedValue))
            {
                return new ValidationResult(false, "'" + value.ToString() + "' is not a real number.");
            }

            if (proposedValue >= 5)
            {
                return new ValidationResult(false, "value must be less than 5.");
            }
        }

        return new ValidationResult(true, null);
    }
}

In the XAML, I am binding each Column with its Corresponding ValidationRule. And that part works just fine.

<DataGrid x:Name="MyDgr" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False" >
<DataGrid.Columns>
    <DataGridTextColumn Header="Column1">
        <DataGridTextColumn.Binding>
            <Binding Path="Col1" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                <Binding.ValidationRules>
                    <data:Column1ValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </DataGridTextColumn.Binding>
    </DataGridTextColumn>
    <DataGridTextColumn Header="Column2">
        <DataGridTextColumn.Binding>
            <Binding Path="Col2" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                <Binding.ValidationRules>
                    <data:Column2ValidationRule />
                </Binding.ValidationRules>
            </Binding>
        </DataGridTextColumn.Binding>
    </DataGridTextColumn>                                
</DataGrid.Columns>

For the DataGrid DataContext, I have created a class:

   public class Mydata
{
    public string Col1 { get; set; }
    public string Col2 { get; set; }
}

For my 2 rows of the DataGrid MyDgr, I create 2 new instances of Mydata objects which I add to an observable collection:

var row1 = new Mydata();
var row2 = new Mydata();

ObservableCollection<MyData> dataColl = new ObservableCollection<MyData>();
dataColl.Add(row1);
dataColl.Add(row2);

myDgr.DataContext = dataColl;

However, I don't know how to make it so that the two rows in my DataGrid will have those additional different Validation rules (entries in the first row can only be integers, while entries in the second row are allowed to be doubles, or really any other Validation rule I would like to set by Row). Any help is highly appreciated!

You can Implement row level validation like shown below:

<DataGrid.RowValidationRules>
  <local:CourseValidationRule ValidationStep="UpdatedValue"/>
</DataGrid.RowValidationRules>

A detailed explanation for same can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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