Simple validation on Cell?

Oct 7, 2008 at 12:08 AM
Hi guys,

is there any one who can show me a simple code how to validate a column? and if column is invalid display some message while using style, i am wokring hard to find solution but fail.

here what i have done, but i am unable to see the error in style.

in Panel Resource i have used below line

 

<StackPanel.Resources>
<Style x:Key="styTextBoxColumnStyleGif" TargetType="{x:Type TextBlock}">
     <Setter Property="Validation.ErrorTemplate">
           <Setter.Value>
               <ControlTemplate>
                     <DockPanel LastChildFill="True">
                              <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14pt" Margin="-15,0,0,0" FontWeight="Bold">*</TextBlock>
                                      <Border BorderBrush="Red" BorderThickness="1">
                                             <AdornedElementPlaceholder />
                                      </Border>
                     <
/DockPanel>
               </ControlTemplate>
          </Setter.Value>
     </Setter>
     <
Style.Triggers>
              <Trigger Property="Validation.HasError" Value="true">
                       <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}" />
                       <Setter Property="Background" Value="LightSalmon"/>
              </Trigger>
      </Style.Triggers>
</Style>
</StackPanel.Resources>

and here is code for Grid

 



 

<toolkit:DataGrid Height="201" Name="grdDetail" Width="643" AutoGenerateColumns="False"  AlternationCount="2" CanUserAddRows="False"  CanUserSortColumns="False" AlternatingRowBackground="LightBlue" GridLinesVisibility="None"
IsSynchronizedWithCurrentItem="False" Background="Transparent" RowHeaderWidth="25" RowBackground="White" BorderThickness="0" BorderBrush="White" SelectionUnit="CellOrRowHeader" >
        <toolkit:DataGrid.Columns
                   <toolkit:DataGridTemplateColumn Header="ID">      
                            <toolkit:DataGridTemplateColumn.CellTemplate>
                                
<DataTemplate>
                                          <
TextBox Text="{Binding Path=ID,Mode=OneWay}" ></TextBox> <!-- this is Identity Column-->
                               </DataTemplate>
                   </toolkit:DataGridTemplateColumn.CellTemplate>
                   </toolkit:DataGridTemplateColumn>
                       <toolkit:DataGridTextColumn ElementStyle="{StaticResource styTextBoxColumnStyleGif}" >
                               <toolkit:DataGridTextColumn.DataFieldBinding>
                                       <Binding Path="Brand" ValidatesOnDataErrors="True">
                                                   <Binding.ValidationRules>
                                                           <Validators:StringRangeValidationRule/>   <!-- See Blow for Code Behinde-->
                                                  </Binding.ValidationRules>
                                       </Binding>
                              </toolkit:DataGridTextColumn.DataFieldBinding>
                      </toolkit:DataGridTextColumn>
                     <toolkit:DataGridTextColumn DataFieldBinding="{Binding Path=Password}" Header="Password"/>
                 </toolkit:DataGrid.Columns>
</toolkit:DataGrid>

 

And here is code behind for StringRangeValidation Rule.

 

public class StringRangeValidationRule : ValidationRule
  {
        public override ValidationResult Validate(object value, CultureInfo culture)
           {
                 ValidationResult result = new ValidationResult(true, null);
                 
string inputString = (value ?? string.Empty).ToString();
                  if (inputString=="aqeel")
                 {
                        result =
new ValidationResult(false, "Please input some value");
                  }
                 return result;
           }
}

 

@Sorry
i was finding the solution from net so thats why you can see many copy past code :D

please guys help me and that will help to all others beginner.

i have tried to understansd the http://www.codeproject.com/KB/WPF/wpfvalidation.aspx

but the problem is i am using GRID and i want some thing in grid.

Regards

AQEEL
Oct 7, 2008 at 12:15 AM

Just more explanation regarding above problem, i can validate the column but problem is that i can not display error in tooltip or you can say unable to see style.

 hope that more help you , to solve my problem


AQEEL

Coordinator
Oct 10, 2008 at 2:11 PM
Is your repro similar to this thread, http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=36722.  If so then it is a known issue.