views:

1010

answers:

1

Hi,

I have a UserControl that contains other controls and TextBox. It has Value property that is bound to TextBox text and has ValidatesOnDataErrors set to True.

When validation error occurs in Value property binding, error template (standard red border) is shown around entire UserControl.

Is there a way to show it around TextBox only? I'd like to be able to use any error template so simply putting border around textbox and binding its color or something to Validation.HasError is not an option.

Here's my code:

<DataTemplate x:Key="TextFieldDataTemplate">
    <c:TextField DisplayName="{Binding Name}" Value="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
</DataTemplate>

<controls:FieldBase x:Name="root">
<DockPanel DataContext="{Binding ElementName=root}">
    <TextBlock Text="{Binding DisplayName}"/>
    <TextBox x:Name="txtBox"                 
             Text="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"
             IsReadOnly="{Binding IsReadOnly}"/>
</DockPanel>

UserControl (FieldBase) is than bound to ModelView which performs validation.

Thanks in advance :)

A: 

Hi, to accomplish this task I've used this solution. It uses converter, that "hides" border by converting (Validation.Errors).CurrentItem to Thickness.

<UserControl ...
     x:Name="myControl"
         Validation.ErrorTemplate="{x:Null}">

    <Grid>

        <Grid.Resources>
            <data:ValidationBorderConverter x:Key="ValidationBorderConverter" />
        </Grid.Resources>

    ...

        <Border Height="24" BorderBrush="#ff0000" BorderThickness="{Binding ElementName=myControl, Path=(Validation.Errors).CurrentItem, Converter={StaticResource ValidationBorderConverter}}">
            <TextBox ToolTip="{Binding ElementName=myControl, Path=(Validation.Errors).CurrentItem.ErrorContent}"
                 ... />
        </Border>

    ...

     </Grid>
</UserControl>

ValidationBorderConverter class is pretty simple:

[ValueConversion(typeof(object), typeof(ValidationError))]
public sealed class ValidationBorderConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value == null) ? new Thickness(0) : new Thickness(1);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
jakubgarfield