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
>