This is a migrated thread and some comments may be shown as answers.

custom validation issue

1 Answer 193 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Randy Hompesch
Top achievements
Rank 1
Randy Hompesch asked on 21 Feb 2017, 12:15 PM

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>

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 24 Feb 2017, 10:34 AM
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 you to write beautiful native mobile apps using a single shared C# codebase.
Tags
DataForm
Asked by
Randy Hompesch
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or