C# WPF Datagrid Validation Rule - Pass Parameters

Viewed 176

I have a Point class as follows:

public class Point
{
    public uint lID;
    public double lX_Coordinate;

    public Point()
    {
        ID = 0;
        X_Coordinate = 0;
    }
    public uint ID
    {
        get
        { return lID; }
        set
        {
            lID = value;
        }
    }
    public double X_Coordinate
    {
        get
        { return lX_Coordinate; }
        set
        {
            lX_Coordinate = value;
        }
    }
}

The Point class constitutes the members of the ObservableCollection PointList that belongs to class Geometry as follows:

public class Geometry
{
    public ObservableCollection<Point> lPointList; 
    public double lLeftBoundary;

    public Geometry()
    {
        lPointList = new ObservableCollection<Point>();
    }
    public ObservableCollection<Point> PointList
    {
        get
        { return lPointList; }
        set
        { lPointList = value; }
    }
    public double LeftBoundary
    {
        get
        { return lLeftBoundary; }
        set
        { lLeftBoundary = value; }
    }
}

I then bind the contents of the PointList to a datagrid. There is a Validation Rule that checks the Point X_Coordinate against the LeftBoundary, which is passed on to the Validation Rule through a ValidationWrapper class as Dependency Objects.

The following is the XAML code:

</DataGridTextColumn>
<DataGridTextColumn Header="X" Width="60" >
    <DataGridTextColumn.HeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="HorizontalContentAlignment" Value="Center" />
        </Style>
    </DataGridTextColumn.HeaderStyle >
    <DataGridTextColumn.Binding>
        <Binding Path="X_Coordinate" UpdateSourceTrigger="LostFocus" ValidatesOnExceptions="True" StringFormat="{}{0:N3}">
            <Binding.ValidationRules>
                <c:ValidationGeometryPointX> 
                    <c:ValidationGeometryPointX.ValidationRuleWrapper>
                        <c:ValidationRuleWrapper LeftBoundary="{Binding Data.LeftBoundary, Source={StaticResource proxy}}" />
                    </c:ValidationGeometryPointX.ValidationRuleWrapper>
                </c:ValidationGeometryPointX>
            </Binding.ValidationRules>
        </Binding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

The following is the ValidationWrapper class:

public class ValidationGeometryPointX : ValidationRule
{
    public ValidationGeometryPointX()
    {
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        double PointCoordinate = 0;

        try
        {
            if (((string)value).Length > 0)
                PointCoordinate = Double.Parse((String)value);
        }
        catch
        {
            return new ValidationResult(false, "Illegal characters.");
        }

        if (PointCoordinate < this.ValidationRuleWrapper.LeftBoundary)
        {
            return new ValidationResult(false, "Point cannot be beyond the side boundaries.");
        }

        return ValidationResult.ValidResult;
    }

    public ValidationRuleWrapper ValidationRuleWrapper
    { get; set; }
}

The code for the window that displays the datagrid is the following:

public partial class GeometryWin : Window
{
    public GeometryWin(Geometry Geometry1)
    {
        InitializeComponent();

        base.DataContext = lGeometry;
        datgrdGeometry.ItemsSource = lGeometry.PointList;
    }
}

The above validation has been achieved successfully.

What I am now struggling to do is to pass the Point.ID parameter to the ValidationWrapper class so as to add another validation condition for X_Coordinate based on the ID. Any help would be much appreciated.

PS. For brevity certain parts of the code have been omitted.

1 Answers
Related