31

i need to remove red rectangle around combobox. I have setup combobox in xaml like (below) this and i`m trying to override of the Validation.ErrorTemplate.

        <ComboBox x:Name="comboPodkategoria" 
                            Margin="0,3,0,0"
                            IsSynchronizedWithCurrentItem="False" 
                            IsEditable="False"
                            ItemsSource="{Binding Source={StaticResource PodKategoriaLookup}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                            SelectedValue="{Binding IDPodKategoria}"
                            DisplayMemberPath="kat_popis" SelectedValuePath="IDPodkat" TabIndex="5" Style="{StaticResource combostyle}">
                            <Validation.ErrorTemplate>
                                <ControlTemplate>
                                </ControlTemplate>
                            </Validation.ErrorTemplate> 
            </ComboBox>

And style for removing red rectangle, but a have some error in xaml saying that Visibility property is not recognized or is not accessible. Style definition is below.

<Style x:Key="combostyle">
<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="Visibility" TargetName="NotValid" Value="Visible"/>
    </Trigger>  
</Style.Triggers>   

Any idea? :(

Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
vikox
  • 347
  • 1
  • 3
  • 7

3 Answers3

87

Use this to modify the Validation.ErrorTemplate

<ControlTemplate x:Key="ComboBoxValidationErrorTemplate">
    <DockPanel>
        <Border BorderBrush="Blue" BorderThickness="4">
            <AdornedElementPlaceholder />
        </Border>
    </DockPanel>
</ControlTemplate>

And then use it in your ComboBox like

<ComboBox Validation.ErrorTemplate="{StaticResource ComboBoxValidationErrorTemplate}"
          ...>

To have no indication of a Validation Error, remove the DockPanel, set Visibility to Collapsed or any other way you like.

Almost forgot, probably the easiest way to remove the "Red Border"

<ComboBox Validation.ErrorTemplate="{x:Null}"
          ...>
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • 1
    Perfect, thank you. The last option is perfect for me, i didn`t know that. – vikox Nov 10 '10 at 07:45
  • 3
    +1 for Validation.ErrorTemplate="{x:Null}". I'm experiencing the joy of a "exactly right" UI element. – BSalita Feb 24 '12 at 10:06
  • 11
    +1 for Validation.ErrorTemplate="{x:Null}", awesome one, Thanks. – Mark Sep 12 '13 at 13:36
  • But is it ok to remove red validation rectangle in such case, doesn't it mean that binding is failing? also shouldn't it be done by OneWay binding since the ItemsSource is bound to a StaticResource? as in : `ItemsSource="{Binding Source={StaticResource PodKategoriaLookup, Mode=OneWay},` – mkb Oct 20 '15 at 08:04
  • 1
    @mkb: It is a visual indication of a validation error on a binding. If you want to modify or remove that visual indication, for whatever reason, then it can be done like in the examples above. – Fredrik Hedblad Oct 20 '15 at 11:49
  • I dont have much experience in WPF, but are there such nonsenses with textboxes? – Wish Jun 07 '16 at 14:26
1

Add your Combobox, Validation.ErrorTemplate="{x:Null}" ; this code is ignore errors.

-1

The setter in your trigger is setting the Visibility property of an element named "NotValid". That is not defined in the XAML you posted. If there is no element named "NotValid", that is your problem.

John Myczek
  • 12,076
  • 4
  • 31
  • 44