红色边框保持在文本框即使在数据输入有效 [英] Red border remains on the TextBox even after the data input is valid

查看:107
本文介绍了红色边框保持在文本框即使在数据输入有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写使用WPF,WPF应用程序框架和MahApps.Metro的应用程序。我有这样一个数据输入窗口中启用验证:

I am writing an app using WPF, WPF Application Framework and MahApps.Metro. I have validation enabled for a data entry window like this:

<controls:MetroWindow x:Class="FinancePlus.Presentations.Views.CustomerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="526"
             xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
             xmlns:presentation="clr-namespace:System.Waf.Presentation;assembly=WpfApplicationFramework"
             presentation:ValidationHelper.IsEnabled="true" presentation:ValidationHelper.IsValid="{Binding IsValid, Mode=OneWayToSource}"
             Title="Customer Editor">
    <Window.Resources>
        <ResourceDictionary>            
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Green.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            </ResourceDictionary.MergedDictionaries>


        </ResourceDictionary>


    </Window.Resources>
    <Grid DataContext="{Binding Customer}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.Resources>
            <Style TargetType="GroupBox">
                <Setter Property="Margin" Value="10,5"></Setter>
            </Style>
        </Grid.Resources>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0">
            <GroupBox Header="Personal Information" DockPanel.Dock="Top">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Top">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Label Content="Title:" Grid.Column="0" Grid.Row="0"  Margin="3" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Grid.Row="0" Height="23"  Margin="3" Name="titleTextBox" 
                             Text="{Binding Path=Title, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, ValidatesOnDataErrors=True}" 
                             VerticalAlignment="Center" MinWidth="120" />
                    <Label Content="Full Name:" Grid.Column="0" Grid.Row="1"  Margin="3" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Grid.Row="1" Height="23"  Margin="3" Name="fullNameTextBox" 
                             Text="{Binding Path=FullName, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, ValidatesOnDataErrors=True}" 
                             VerticalAlignment="Center" MinWidth="120" />
                    <Label Content="Name With Initials:" Grid.Column="0" Grid.Row="2"  Margin="3" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Grid.Row="2" Height="23"  Margin="4" Name="nameWithInitialsTextBox" 
                             Text="{Binding Path=NameWithInitials, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, ValidatesOnDataErrors=True}" 
                             VerticalAlignment="Center" MinWidth="120" />
                    <Label Content="Civil Status:" Grid.Column="0" Grid.Row="3"  Margin="3" VerticalAlignment="Center" />
                    <ComboBox DisplayMemberPath="CivilStatus" Grid.Column="1" Grid.Row="3" Height="23"  
                      ItemsSource="{Binding}" Margin="3" Name="civilStatusComboBox" VerticalAlignment="Center" MinWidth="120" HorizontalAlignment="Stretch">
                        <ComboBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel />
                            </ItemsPanelTemplate>
                        </ComboBox.ItemsPanel>
                    </ComboBox>
                    <Label Content="Date Of Birth:" Grid.Column="0" Grid.Row="4"  Margin="3" VerticalAlignment="Center" />
                    <DatePicker Grid.Column="1" Grid.Row="4" Height="25"  Margin="3" Name="dateOfBirthDatePicker" HorizontalAlignment="Right"
                        SelectedDate="{Binding Path=DateOfBirth, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Center" Width="115" />                    
                    <Label Content="Id Number:" Grid.Column="0" Grid.Row="5"  Margin="3" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Grid.Row="5" Height="23"  Margin="3" Name="idNumberTextBox" 
                             Text="{Binding Path=IdNumber, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, ValidatesOnDataErrors=True}" 
                             VerticalAlignment="Center" MinWidth="120" />                    
                    <Label Content="Profession:" Grid.Column="0" Grid.Row="6"  Margin="3" VerticalAlignment="Center" />
                    <TextBox Grid.Column="1" Grid.Row="6" Height="23"  Margin="3" Name="professionTextBox" 
                             Text="{Binding Path=Profession, Mode=TwoWay, ValidatesOnExceptions=true, NotifyOnValidationError=true, ValidatesOnDataErrors=True}" 
                             VerticalAlignment="Center" MinWidth="120" />                    
                </Grid>
            </GroupBox>

..... More code.



结果是这样的:

The result looks like this:

这看起来不错。麻烦的是,甚至当我为文本框红色边框保持输入有效的值。就像你可以在标题和完整名称文本框见在这里上课。我如何删除这个残留红色边框?它在哪里来的?

Which looks nice. The trouble is even when I enter valid values for a TextBox a red border remains. Like you can see in the title and full name TextBoxes here. How do I remove this residue red border? Where is it coming from?

推荐答案

我以前也碰到过这样的,当它发生在我身上我不得不在<$ C来执行数据变化$ C>初始化和 ContentRendered 。因为我有项目实际上约束。

I've come across this before, when it happened to me I had to perform a data change during initialize and ContentRendered. Cause I had the items actually bound.

我看到前一段时间对堆栈溢出的另一件事是,在这里发生一个问题,也造成了类似的事情。

Another thing I saw awhile ago on Stack Overflow was an issue that happened here that also caused something similar.

<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <ControlTemplate.Resources>
                <BooleanToVisibilityConverter x:Key="converter" />
        </ControlTemplate.Resources>
            <DockPanel LastChildFill="True">
                <Border 
                    BorderThickness="1"
                    BorderBrush="Red"
                    Visibility="{Binding ElementName=placeholder, Mode=OneWay, Path=AdornedElement.IsVisible, Converter={StaticResource converter}}">
                    <AdornedElementPlaceholder x:Name="placeholder" />
                </Border>
             </DockPanel>
         </ControlTemplate>
    </Setter.Value>
</Setter>

这是我的遭遇,如果不帮忙,我会尝试重现该问题并修复为好。看我能不能帮你出好一点的话,描述我是如何固定它。但希望这两项建议都能帮助你。

That was my encounter, if that doesn't help I'll attempt to recreate the issue and fix as well. See if I can help you out a little better then describing how I fixed it. But hopefully one of those two suggestions helps you out.

只要让我知道,希望它帮助。

Just let me know, hope it helps.

这篇关于红色边框保持在文本框即使在数据输入有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆