繁体   English   中英

WPF中的文本框的自定义验证

[英]Custom validation of textbox in WPF

因此,我尝试通过以下示例进行操作: 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。 当我失去对文本框的关注时,什么也没有发生。 不管我输入什么。 有任何想法吗? :)

您设置了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" />

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

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM