即使数据输入有效,红色边框仍保留在TextBox上

时间:2013-03-13 05:06:39

标签: c# wpf

我正在使用WPF,WPF应用程序框架和MahApps.Metro编写应用程序。我为这样的数据输入窗口启用了验证:

<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.

结果如下:

enter image description here

哪个看起来不错。即使我输入TextBox的有效值仍然存在红色边框,问题就出现了。就像你在标题和全名TextBox es中看到的一样。如何清除这个残留的红色边框?它来自哪里?

enter image description here

1 个答案:

答案 0 :(得分:0)

我之前遇到过这种情况,当发生在我身上时,我必须在initializeContentRendered期间执行数据更改。因为我有物品实际绑定。

我之前在Stack Overflow上看到的另一件事是这里发生的一个问题,也引起了类似的问题。

<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>

那是我的遭遇,如果这没有帮助,我会尝试重新创建问题并修复。看看我是否可以帮助你更好地描述我如何修复它。但希望这两个建议中的一个可以帮助你。

请告诉我,希望有所帮助。