6

我正在使用 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.

结果如下所示:

在此处输入图像描述

看起来不错。TextBox问题是即使我为红色边框输入有效值仍然存在。就像您可以在此处看到的标题和全名TextBoxes 一样。如何去除这个残留的红色边框?它来自哪里?

在此处输入图像描述

4

1 回答 1

0

我以前遇到过这种情况,当它发生在我身上时,我不得不在initializeand期间执行数据更改ContentRendered。因为我有这些项目实际绑定。

我不久前在 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>

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

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

于 2013-03-13T05:38:22.123 回答