【问题标题】:Custom validation of textbox in WPFWPF中文本框的自定义验证
【发布时间】:2011-05-20 11:51:26
【问题描述】:

所以我试图在这个例子中做点什么:http://www.codeproject.com/KB/WPF/wpfvalidation.aspx

我的文本框目前如下所示:

<TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120"  >
                <TextBox.Text>

                    <Binding Path="Name" UpdateSourceTrigger="LostFocus">
                        <Binding.ValidationRules>
                            <validators:TextRangeValidator
                        MinimumLength="10"
                        MaximumLength="10"
                        ErrorMessage="ID has to be 10 letters" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>

            </TextBox>

我直接从该网站上的示例中复制了我的 TextRangeValidator。当我失去对文本框的关注时,什么也没有发生。不管我输入什么。有任何想法吗? :)

【问题讨论】:

    标签: wpf validation textbox


    【解决方案1】:

    您是否设置了 Validation.ErrorTemplate?它在示例中的 Application.Resources 中定义如下。您可能已经错过了

    <Application.Resources>
    
            <Style TargetType="{x:Type TextBox}">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <DockPanel LastChildFill="True">
    
                                <TextBlock DockPanel.Dock="Right"
                                    Foreground="Orange"
                                    Margin="5" 
                                    FontSize="12pt"
                                    Text="{Binding ElementName=MyAdorner, 
                               Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                </TextBlock>
    
                                <Border BorderBrush="Green" BorderThickness="3">
                                    <AdornedElementPlaceholder Name="MyAdorner" />
                                </Border>
    
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={RelativeSource Self}, 
                       Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    
        </Application.Resources>
    

    编辑

    您的默认值不会触发验证例程。要强制它验证默认值,您必须设置

    <validators:TextRangeValidator ValidatesOnTargetUpdated="True"
                            MinimumLength="10"
                            MaximumLength="10"
                            ErrorMessage="ID has to be 10 letters" />
    

    【讨论】:

    • 我没有这样做,因为我认为它会使用默认值。它在哪里使文本框的边框变红?我不知道在哪里放置这段代码 - 这就是我没有使用它的原因,呵呵..我是 WPF 的新手。
    • 其实应该会出现默认的红色边框,但是不会显示你的错误信息
    • 应该去哪里?我在里面放什么?
    • 是应用程序级别的资源,可在您的应用程序中使用。它位于 App.xaml 文件中
    • 啊,谢谢,但我们认为它并没有太大变化。还有其他想法吗? :)
    【解决方案2】:

    我认为您需要在 Text 绑定中设置 ValidatesOnDataErrors=True 才能使其工作

    【讨论】:

    • 文本框似乎没有该属性?或者你会在哪里添加它?
    【解决方案3】:

    WPF 文本框验证

    <Style x:Key="TextBoxInError" TargetType="TextBox">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel>                        
    
                            <Grid>
                                <Polygon Points="20,10,20,0 0,0"
                                Stroke="Black"
                                StrokeThickness="1"
                                Fill="Red"
                                HorizontalAlignment="Right"
                                VerticalAlignment="Top"
                                ToolTip="{Binding ElementName=adorner, 
                                   Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
                                <AdornedElementPlaceholder x:Name="adorner"/>
    
    
                            <AdornedElementPlaceholder Name="customAdorner"  VerticalAlignment="Center" >
                                <Border BorderBrush="red" BorderThickness="1" />
                            </AdornedElementPlaceholder>
    
    
                            </Grid>
    
                            <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" 
                                Width="150" Height="20" CornerRadius="5"
                                ToolTip="{Binding ElementName=customAdorner, 
                                          Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                <TextBlock Text= "{Binding ElementName=customAdorner, 
                                          Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" VerticalAlignment="center" HorizontalAlignment="Left" 
                                   FontWeight="Bold" Foreground="white" Width="250" />                               
    
    
                            </Border>
    
    
                        </DockPanel>
    
    
    
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="ToolTip" 
                        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=(Validation.Errors)[0].ErrorContent}" />
    
                </Trigger>
            </Style.Triggers>        
    
    
        </Style>
    

    【讨论】:

    • 你需要说出你实际做了什么来提供一些关于这是一个答案的指导,而不仅仅是发布代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多