TextBox中的输入验证

问题描述 投票:0回答:2

不希望在启动时显示错误消息/打开选项卡。

使用IDataErrorInfo(到VM)实现验证,定义了ErrorTemplate(XAML)并且能够在TextBox旁边获取错误消息。

VM:(extended IDataErrorInfo)
public string this[string propertyName]
        {
            get
            {
                string validationResult = null;
                switch (propertyName)
                {
                case "ProjectManager":
                validationResult = ValidateManagerName();
                break;
                }
                return validationResult;
            }
        }
        public string ValidateManagerName()
        {
            if (string.IsNullOrEmpty(this.ProjectManager))
            {
                return "Manager name is mandatory!";
            }
            else
                return string.Empty;
        }
XAML.cs
save_option_summ()
{
  BindingExpression be3 = managername.GetBindingExpression(TextBox.TextProperty);
            be3.UpdateSource();
}
<StackPanel.Resources>
            <ControlTemplate x:Key="ErrorTemplate">
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right" Foreground="Blue" FontSize="13" Text="{Binding ElementName=adorned,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
                    </TextBlock>
                    <Border BorderBrush="Red" BorderThickness="2">
                        <AdornedElementPlaceholder x:Name="adorned"/>
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </StackPanel.Resources>

 <StackPanel Orientation="Horizontal" Margin="0,5,0,0">
            <Label Content="Project Manager" FontSize="14" Margin="100,0,0,0" Width="150" FontFamily="Calibri"></Label>
            <TextBox  Height="auto" Width="300" Background="White"  Margin="100,0,0,0" Validation.ErrorTemplate = "{StaticResource ResourceKey=ErrorTemplate}" Name="managername" Text="{Binding ProjectManager,Mode=TwoWay,UpdateSourceTrigger=LostFocus,ValidatesOnDataErrors=True}" TextChanged="TextChanged"  FontFamily="Calibri" FontSize="14"/>
        </StackPanel>
c# wpf validation mvvm textbox
2个回答
0
投票

您应该修改TextBox样式,以便显示该属性的错误。这是一个将错误显示为工具提示的简单示例:

 <TextBox>
        <TextBox.Style>
            <Style TargetType="TextBox">
                <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>
        </TextBox.Style>
    </TextBox>

0
投票

所以,随着shivam sood(有助于在工具提示中显示错误)答案。

在显示的代码(问题)中,

public string ValidateManagerName()
{
   if (string.IsNullOrEmpty(this.ProjectManager))
        {
            return "string.Empty";
        }
        //Other conditions can be written like alphanumeric cherck
        else
            return string.Empty;
    }

在null检查(第一个条件)中不返回任何内容,因此文本框不会总是显示错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.