custom validation issue

2 posts, 0 answers
  1. Randy Hompesch
    Randy Hompesch avatar
    70 posts
    Member since:
    Nov 2008

    Posted 21 Feb Link to this post

    Hi,

    I have a fairly simple scenario that I am having trouble with. I have a custom data annotation as shown below. It just makes sure that at least one item in my collection has it's IsSelected flag set.  This collection is bound to an ItemsControl of checkboxes. The xaml is at the bottom. The validation works correctly but the error message displayed in  summary at the bottom if the form is not red and is prefixed with a colon. Is there a way to make this a little better and more consistent with "normal" dataannotation validations so that the ItemsControl or containing stackpanel is highlighted in a similar fashion as a textbox might be highlighted?

    Thanks ... Ed

    public class HasOneItem : ValidationAttribute
    {
        public HasOneItem() {}
     
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                ObservableCollection<SampleTechniqueInfo> sti = (ObservableCollection<SampleTechniqueInfo>)value;
                if ((from a in sti where a.IsSelected == true select a).Count() == 0)
                {
                    var errorMessage = FormatErrorMessage(validationContext.DisplayName);
                    return new ValidationResult(errorMessage);
                }
            }
            return ValidationResult.Success;
        }
    }
     
     
    <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="2" Grid.RowSpan ="5">
        <Label Content="Associated Techniques:"  HorizontalAlignment="Left" HorizontalContentAlignment="Left"  Width="350" Height="auto"/>
        <StackPanel Orientation="Vertical" MaxHeight="210" >
            <ItemsControl x:Name ="lstTechniques" ItemsSource="{Binding AssociatedTechniques }"
                            Margin="5,0,0,0"
                            >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
     
                        <support:UniformGridWithOrientation Orientation="Vertical"  Columns="4"  />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate >
                        <CheckBox Content="{Binding TechniqueAbbr}"
                                    IsChecked="{Binding IsSelected, Mode=TwoWay}"
                                    ToolTip="{Binding TechniqueName}"
                                          
                            >
     
                        </CheckBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
     
        </StackPanel>
    </StackPanel>
  2. Yoan
    Admin
    Yoan avatar
    1128 posts

    Posted 24 Feb Link to this post

    Hi Randy,

    Can you try using the second overload of the ValidationResults which takes a list of members that have validation errors:
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
       {
           if (value != null)
           {
                  string[] memberNames = new string[] { validationContext.MemberName };
               ObservableCollection<SampleTechniqueInfo> sti = (ObservableCollection<SampleTechniqueInfo>)value;
               if ((from a in sti where a.IsSelected == true select a).Count() == 0)
               {
                   var errorMessage = FormatErrorMessage(validationContext.DisplayName);
                   return new ValidationResult(errorMessage,memberNames);
               }
           }
           return ValidationResult.Success;
       }

    Please let me know how this works for you.

    Regards,
    Yoan
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top