- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题可能很简单,但是我找不到办法做,所以我问了。
我在我的模型中使用 IDataErrorInfo 来验证它如下:
public class Group : INotifyPropertyChanged, IDataErrorInfo
{
//Fileds & Properties
public string Error
{
get { return String.Empty; }
}
public string this[string columnName]
{
get
{
if (columnName == "GroupName")
{
bool _IsDuplicateGroupName;
using (MunimPlusContext context = new MunimPlusContext())
{
_IsDuplicateGroupName = context.GroupSet.Any(x => x.GroupName.ToLower() == GroupName.ToLower());
}
if (String.IsNullOrWhiteSpace(GroupName))
{
return "Group Name cannot be Empty.";
}
else if (_IsDuplicateGroupName)
{
return "Duplicate Group Name. Please choose a unique Group Name.";
}
}
if (columnName == "ParentId")
{
if (ParentId == null)
{
return "Please select Under Group under which " + (GroupName == null ? "this" : GroupName) + " Group will appear.";
}
else if (ParentId <= 0)
{
return "Please select a valid GroupName from the list.";
}
}
if (columnName == "NatureOfGroupId")
{
Group _PrimaryGroup;
using (MunimPlusContext context = new MunimPlusContext())
{
_PrimaryGroup = context.GroupSet.Where(x => x.GroupName == "Primary").FirstOrDefault();
}
if (_PrimaryGroup.GroupId == ParentId)
{
if (NatureOfGroupId == null)
{
return "Please select the Nature of Group.";
}
else if (NatureOfGroupId <= 0)
{
return "Please select a valid Nature of Group from the list.";
}
}
}
return String.Empty;
}
}
}
现在,在我的 XAML 中:
<Grid.Resources>
<DataTemplate DataType="{x:Type ValidationError}">
<TextBlock FontStyle="Italic" Foreground="Red" HorizontalAlignment="Right" Text="{Binding ErrorContent}" Margin="4,0,4,4" />
</DataTemplate>
</Grid.Resources>
<TextBlock Grid.Row="3" Grid.Column="1" Text="Name" />
<TextBlock Grid.Row="3" Grid.Column="1" Text=":" />
<TextBox Grid.Row="3" Grid.Column="2" x:Name="txtGroupName" Text="{Binding GroupName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
<ContentPresenter Grid.Row="4" Grid.Column="2" Content="{Binding ElementName=txtGroupName, Path=(Validation.Errors).CurrentItem}" />
<TextBlock Grid.Row="5" Grid.Column="1" Text="Alias" />
<TextBlock Grid.Row="5" Grid.Column="1" Text=":" />
<TextBox Grid.Row="5" Grid.Column="2" x:Name="txtAlias" Text="{Binding Alias, ValidatesOnDataErrors=True}"/>
<ContentPresenter Grid.Row="6" Grid.Column="2" Content="{Binding ElementName=txtAlias, Path=(Validation.Errors).CurrentItem}" />
<TextBlock Grid.Row="7" Grid.Column="1" Text="Under" />
<TextBlock Grid.Row="7" Grid.Column="1" Text=":" />
<ComboBox Grid.Row="7" Grid.Column="2" x:Name="cmbParentGroup"
ItemsSource="{DynamicResource Items}" SelectedValue="{Binding ParentId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Group.GroupId" Grid.IsSharedSizeScope="True" TextSearch.TextPath="Group.GroupName" LostFocus="cmbParentGroup_LostFocus"/>
<ContentPresenter Grid.Row="8" Grid.Column="2" Content="{Binding ElementName=cmbParentGroup, Path=(Validation.Errors).CurrentItem}" />
<TextBlock Grid.Row="9" Grid.Column="1" Text="Nature Of Group" />
<TextBlock Grid.Row="9" Grid.Column="1" Text=":" />
<ComboBox Grid.Row="9" Grid.Column="2" x:Name="cmbNatureOfGroup"
ItemsSource="{Binding DataContext.NaturesOfGroup, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type core:UserControlViewBase}}}"
DisplayMemberPath="Nature" SelectedValue="{Binding NatureOfGroupId, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="NatureOfGroupId" LostFocus="cmbNatureOfGroup_LostFocus"/>
<ContentPresenter Grid.Row="10" Grid.Column="2" Content="{Binding ElementName=cmbNatureOfGroup, Path=(Validation.Errors).CurrentItem}" />
<StackPanel Grid.Row="11" Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Save" Style="{StaticResource SaveButtonWithText}"
Command="{Binding DataContext.SaveCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type core:UserControlViewBase}}}"/>
<Button Content="Cancel" Style="{StaticResource CancelButtonWithText}"
Command="{Binding DataContext.CancelCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type core:UserControlViewBase}}}"/>
</StackPanel>
现在,当我将数据保存到数据库时,我想检查我的模型是否有效。我该怎么做?
最佳答案
终于从this得到了我的答案发布。
这是我的 IDataErrorInfo 实现代码:
public string Error
{
get { return null; }
}
public string this[string propertyName]
{
get { return GetValidationError(propertyName); }
}
private static readonly string[] ValidatedProperties = { "GroupName", "ParentId", "NatureOfGroupId" };
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
{
if (GetValidationError(property) != null)
{
return false;
}
}
return true;
}
}
private string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "GroupName":
bool _IsDuplicateGroupName;
using (MunimPlusContext context = new MunimPlusContext())
{
_IsDuplicateGroupName = context.GroupSet.Any(x => x.GroupName.ToLower() == GroupName.ToLower());
}
if (String.IsNullOrWhiteSpace(GroupName))
{
error = "Group Name cannot be Empty.";
}
else if (_IsDuplicateGroupName)
{
error = "Duplicate Group Name. Please choose a unique Group Name.";
}
break;
case "ParentId":
if (ParentId == null)
{
error = "Please select Under Group under which " + (GroupName == null ? "this" : GroupName) + " Group will appear.";
}
else if (ParentId <= 0)
{
error = "Please select a valid GroupName from the list.";
}
break;
case "NatureOfGroupId":
Group _PrimaryGroup;
using (MunimPlusContext context = new MunimPlusContext())
{
_PrimaryGroup = context.GroupSet.Where(x => x.GroupName == "Primary").FirstOrDefault();
}
if (_PrimaryGroup.GroupId == ParentId)
{
if (NatureOfGroupId == null)
{
error = "Please select the Nature of Group.";
}
else if (NatureOfGroupId <= 0)
{
error = "Please select a valid Nature of Group from the list.";
}
}
break;
}
return error;
}
下面是我如何检查是否所有属性都对名为 CurrentGroup 的对象有效?
if (CurrentGroup.IsValid)
{
//save data to the database.......
}
关于c# - 如何在使用 IDataErrorInfo 时检查模型是否有效或有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29356839/
我正在使用 MVVM 在 WPF 项目中工作. 使用 IDataErrorInfo 验证错误的最佳做法是什么? ?在我的模型或 View 模型中? 用于实现验证的最佳模式是什么? 附注我正在使用 .N
我正在创建一个将使用 IDataErrorInfo 的 WPF 应用程序我的业务对象实现的数据验证。我一直在使用 this demo from a blog post了解ValidatesOnData
注意-我的类(class)是EntityObject上课! 我有以下类(class): public class Foo { public Bar Bar { get; set; } } pu
在 MVVM IDataErrorInfo用于验证。在这个时间索引器检查值不能得到。在调用它后如何检查我无法获取的值。代码在这里。列名中传递的值是什么。 public string this[stri
我有一个简单定义如下的地址对象: public class Address { public string StreetNumber { get; set; } public stri
自从我尝试学习 WPF 以来,我越来越多地看到使用接口(interface) IDataErrorInfo 将错误绑定(bind)到接口(interface)。我的问题是我通常将数据验证放在属性的 s
我不知道是我找不到它还是它不存在,但是是否有任何验证属性可以检查集合是否为空/空? 如果没有,是否有关于如何创建我自己的验证属性的好资源? 最佳答案 您好,当您按照默认使用 DataAnnotatio
昨天,我问了一个关于 MVVM 验证的问题,有人用一段代码回答:https://stackoverflow.com/a/13387724/ 我试图理解这段代码,但我就是不理解索引器部分。有人可以向我解
我有一个绑定(bind)到 winform 的对象,这个对象实现了 IDataErrorInfo。我有一个错误提供者。问题是当一个属性的一个属性发生变化时。 当我更改年龄时没有问题(即检查并正确显示/
好吧,现在我正在验证数据。我有这段代码: 在我看来:
我在使用 IDataErrorInfo 验证 ComboBox 时遇到问题。 我设置了 1 个文本框和 1 个组合框,在运行程序时,第一个焦点在文本框上,当我点击 Tab 以聚焦在组合框中时,我得到:
这个问题可能很简单,但是我找不到办法做,所以我问了。 我在我的模型中使用 IDataErrorInfo 来验证它如下: public class Group : INotifyPropertyChan
我正在为 WPF 应用程序编写(尝试编写)单元测试。 UI 绑定(bind)以实现 IDataErrorInfo 的业务对象,这样当我在 View xaml 中设置 ValidatesOnDataEr
DataAnnotations 与 IDataErrorInfo 两者的优点和缺点? 一个比另一个的好处? (尤其是与 MVC 相关的) 最佳答案 因为我不想开始一个新问题,所以迟到了讨论。我的出发点
目前我的业务对象实现IDataErrorInfo .由于我打算在 ASP.NET MVC 3 中使用这些库,我认为我应该实现 IValidatableObject也可以代替。 WPF 是否与 IVal
我在某些面板上有两个控件:文本框和组合框: 当我在组合框上选择某些内容时,我需要强制对文本框进行 idataerrorinfo 验证。怎么做? View 模型中的代码: string IDataE
我们在我的 MVVM 应用程序中有一个配置设置页面。此处的字段之一(属性:BackupFolderPath)有一个 TextBox 控件。我们已经使用 IDataErrorInfo 对该控件进行了验证
我是 WPF MVVM 的新手,想对本文提出一个后续问题: Enable Disable save button during Validation using IDataErrorInfo 如果表单
我有 ItemType ,它在 IDataErrorInfo 接口(interface)的帮助下实现了验证所需的一切: #region IDataErrorInfo implementation
我有一个实现 IDataError 的 View 模型。在 View 模型中,我有一个 ObservableCollection。 ObservableCollection 在我的 View 中填充了
我是一名优秀的程序员,十分优秀!