0

When data is validated it comes as false and therefore textbox properties are changed and error is shown

Is it possible to do the opposite when data is valid the text box would become green and (check-mark) would appear next to the text box. Something like this

Xaml

<Window.Resources>
        <ControlTemplate x:Key="eTemplate">
            <DockPanel LastChildFill="True">
                <TextBlock DockPanel.Dock="Right" Foreground="Blue" FontSize="13" Text="{Binding ElementName=adorned,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
                </TextBlock>
                <Border BorderBrush="Red" BorderThickness="2">
                    <AdornedElementPlaceholder x:Name="adorned"/>
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>
dkozl
  • 32,814
  • 8
  • 87
  • 89
newb
  • 41
  • 7

1 Answers1

0

Haven't tried this, but I think the best way would be to go ahead and style your normal textbox template to show the green border and the checkmark

Put the border and the checkmark invisible by default.

Use triggers to make the stuff show when there is no focus and the user has entered something into the textbox.

Something like this?:

<ControlTemplate.Triggers>
   <MultiTrigger>
      <MultiTrigger.Conditions>
         <Condition Property="IsFocused" Value="False" />
         <Condition Property="Text"  Value="" />
      </MultiTrigger.Conditions>
      <Setter Property="Visibility" TargetName="border" Value="Visible" />
      <Setter Property="Visibility" TargetName="checkmark" Value="Visible" />
   </MultiTrigger>
</ControlTemplate.Triggers>

Reference Article

Control Template might look something like this:

<TextBox.Template>
   <ControlTemplate>
      <Grid>
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="107*"/>
            <ColumnDefinition Width="23*"/>
         </Grid.ColumnDefinitions>
         <TextBox Grid.ColumnSpan="1" />
         <Border Name="border" BorderBrush="DarkGreen" BorderThickness="1" Grid.ColumnSpan="1" Visibility="Hidden">
         </Border>
         <Image Name="checkmark" Grid.Column="1" Source="Images/check_48.ico" Grid.ColumnSpan="2" Visibility="Hidden"  />
      </Grid>
      <ControlTemplate.Triggers>
         <MultiTrigger>
            <MultiTrigger.Conditions>
               <Condition Property="IsMouseOver" Value="True" />
            </MultiTrigger.Conditions>
            <Setter Property="Visibility" TargetName="border" Value="Visible" />
            <Setter Property="Visibility" TargetName="checkmark" Value="Visible" />
         </MultiTrigger>
      </ControlTemplate.Triggers>
   </ControlTemplate>
</TextBox.Template>

In my sample...I have the border and the checkmark only showing when the mouse is over the item...You would have it showing when it is NOT empty...and it is not focused like above.

Bubba
  • 275
  • 1
  • 10
  • Could you explain it more in detail? – newb Dec 31 '15 at 18:15
  • @Buba so this is happening only in XAML, do I need to do anything in the back-end? – newb Dec 31 '15 at 20:19
  • 1
    Yeah...your validation is taken care of with 'IDataErrorInfo' and the data binding. Your question really doesn't have anything to do with validation. It is basically, _when not empty_ and _when not focused_. When there is a validation error, your normal template won't show. – Bubba Dec 31 '15 at 20:23
  • You could also set the trigger up to `Property="Validation.HasError" Value="False"` and not empty, I reckon – Bubba Dec 31 '15 at 20:27
  • So if the the validation.haserror property is false meaning it doesn't have any errors the style will be changed to green border and check-mark next to it? – newb Dec 31 '15 at 20:43
  • @Buba I think there is another solution basically in the validation checking if I add if the input is valid and add image and get; the text box and change the style to whatever, do you think it would work? – newb Dec 31 '15 at 20:53