remove red rectangle around combobox

2020-02-17 10:01发布

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? :(

3条回答
老娘就宠你
2楼-- · 2020-02-17 10:28

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}"
          ...>
查看更多
萌系小妹纸
3楼-- · 2020-02-17 10:41

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.

查看更多
不美不萌又怎样
4楼-- · 2020-02-17 10:49

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

查看更多
登录 后发表回答