This project is read-only.
1
Vote

validation issue

description

Hi Anybody?
I put validation into my extendeddatagridview, but it doesn't work.
Following is what I tried:
Added following style in the resourcedictionary:
<Style x:Key="TextBlockStyle" TargetType="{x:Type xGrid:DataGridCell}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True" >
                    <Setter Property="Background" Value="Pink"/>
                    <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
Added following in the Column section:
<xxDGColumn:ExtendedDataGridTextColumn Width="50"
                                                       MinWidth="10"
                                                       AllowAutoFilter="True"
                                                       Binding="{Binding Path=item1,
                                                                         Mode=TwoWay, 
                                                       ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
                                                       CellStyle = "{StaticResource  ResourceKey=TextBlockStyle}" 
                                                       Header="Item1*"
                                                       HeaderStyle="{StaticResource dgColumnHeaderColor}"
                                                       IsReadOnly="False" />
Also added IDataErrorInfo in the view model with following:

public override string Error
    {
        get
        {
            return "....";
        }
    }

    public bool HasError
    {
        get
        {
            string tmp = String.Empty;
            tmp += this["item1"];
            tmp += this["item2"];

            return (tmp.Length > 0);
        }
    }
    /// <summary>
    /// Will be called for each and every property when ever its value is changed
    /// </summary>
    /// <param name="columnName">Name of the property whose value is changed</param>
    /// <returns></returns>
    public override string this[string columnName]
    {
        get
        {
            return Validate(columnName);
        }
    }

    private string Validate(string propertyName)
    {
        // Return error message if there is error on else return empty or null string
        string validationMessage = string.Empty;

        switch (propertyName)
        {
            case "item1": // property name
                {
                    if (string.IsNullOrEmpty(item1))
                    {
                         validationMessage = "item1 not be null.";
                    }
                }
                break;
            case "item2": // property name
                {
                    if (string.IsNullOrEmpty(item2))
                    {
                          validationMessage = "item2 can not be null.";
                    }
                }
                break;
        }

        return validationMessage;
    }
Does it have any code that prevents from doing the right thing? Please advise.
Am I doing anything wrong here?

thank you
Ak

comments