【问题标题】:Validation for wpf without MVVM patern验证没有 MVVM 模式的 wpf
【发布时间】:2015-11-06 14:12:30
【问题描述】:

我正在使用 WPF 并使用代码隐藏文件绑定 DataContext。我没有遵循 MVVM 或任何其他模式。我想在我的表单中添加验证。我使用数据 System.ComponentModel.DataAnnotations 进行验证。下面是代码文件

[PropertyChanged.ImplementPropertyChanged]
public class Person : PropertyValidateModel
{
    public long Id { get; set; }

    [Required]
    public string Name { get; set; }
    public long Score { get; set; }
}


[PropertyChanged.ImplementPropertyChanged]
public abstract class PropertyValidateModel : IDataErrorInfo
{
    // check for general model error    
    public string Error { get { return null; } }

    // check for property errors    
    public string this[string columnName]
    {
        get
        {
            var validationResults = new List<ValidationResult>();

            if (Validator.TryValidateProperty(
                    GetType().GetProperty(columnName).GetValue(this)
                    , new ValidationContext(this)
                    {
                        MemberName = columnName
                    }
                    , validationResults))
                return null;

            return validationResults.First().ErrorMessage;
        }
    }
}  

我的 XAML 文件如下:

<Window x:Class="WPFDataContext.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel HorizontalAlignment="Left">
        <TextBox Width="100" Text="{Binding Path=Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <TextBox Width="100" Text="{Binding Path=Score, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <Button Name="btnSave" Click="btnSave_Click" Content="Save" ></Button>
    </StackPanel>
</Grid>

这会向表单添加验证,但如果我想检查模型是否有效,那么我该如何检查?请帮助我,因为我没有获得我在 MVC 模式中用于 ModelState.IsValid 的 IsValid 属性。请帮忙。

【问题讨论】:

  • 为什么不使用 MVVM?您必须非常努力地解决它。
  • 我没有 MVVM 的基本知识,而且我的项目不是在 MVVM 模式中构建的,所以请帮助我。
  • 你能用 WinForms 代替 WPF 吗?
  • 没有。我无法使用 winform。
  • wpf without MVVM patern - 没有这样的东西。忘了它。也就是说,您在这里使用 DataBinding 和 IDataErrorInfo,即我书中的 MVVM。

标签: c# wpf validation xaml silverlight


【解决方案1】:

您可以使用 TextBox 的 lostfocus 事件,例如您想要 texbox 的电子邮件验证

你的 XAML

<TextBox x:Name="txtEmail1" Grid.Column="1" Grid.Row="7" MaxLength="70"  Margin="0,5,5,0" Style="{StaticResource  md-3s-textbox}" HorizontalAlignment="Left"  LostFocus="TxtEmail1_LostFocus"  >

你的代码背后

 public static bool IsValidEmail( string email)
        {
            string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|" + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)" + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
            var regex = new Regex(pattern, RegexOptions.IgnoreCase);
            return regex.IsMatch(email);
        }

    private void TxtEmail1_LostFocus(object sender, RoutedEventArgs e)
    {
        if (IsValidEmail(txtEmail1.Text.ToString().Trim())==false)
        {
            txtEmail1.ToolTip = "Invalid Email";
            txtEmail1.BorderBrush= System.Windows.Media.Brushes.Red;
        }
        
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2012-12-27
    • 1970-01-01
    • 2014-01-29
    • 2010-12-25
    相关资源
    最近更新 更多