我没有绑定错误,此代码在另一个地方工作.我还没有发现我现在所做的与它工作的代码有什么不同,而且代码不是那么多.
在UserControl.Resource中:
<Style TargetType="TextBox">
<Setter Property="BorderBrush" Value="DarkBlue"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Margin" Value="0,1,0,1"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder/>
<Grid Margin="2,0,0,0">
<Ellipse Width="20" Height="20" Fill="Red"/>
<TextBlock Foreground="White" Text="X" FontWeight="Bold"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</StackPanel>
</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>
Run Code Online (Sandbox Code Playgroud)
下面也在Xaml中:
<TextBlock Height="23" HorizontalAlignment="Left" Margin="22,90,0,0"
Text="Keywords" VerticalAlignment="Top"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="22,108,0,0"
VerticalAlignment="Top" Width="244">
<Binding Path="Tags" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<DataErrorValidationRule ValidatesOnTargetUpdated="False"/>
</Binding.ValidationRules>
</Binding>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
我的ViewModel中的按钮SAVE仅在Model.Tags属性从用户输入10个字符时激活.当我输入10,11然后返回8个字符时,按钮激活/禁用工作正常.所有属性更改都会被触发.
型号:
namespace …Run Code Online (Sandbox Code Playgroud) 用于MVC 3的当前验证方法似乎是ValidationAttributes.我有一个非常特定于该模型的类验证,并且具有一些属性之间的交互.
基本上,该模型具有其他模型的集合,并且它们以相同的形式进行编辑.我们称之为ModelA,它有一个ModelB的集合.我可能需要验证的一件事是,ModelB的某些属性的总和小于ModelA的属性.用户可以在一些选项中划分X个点数.
ValidationAttributes非常通用,我不确定它们是否适合这项工作.
我不知道MVC 3中是如何支持IDateErrorInfo的,以及它是否可以直接使用.
一种方法是通过方法验证,但这意味着我不能进行客户端验证.
做这样的事情的正确方法是什么?我还有其他选择吗?我低估了ValidationAttribute的强大功能吗?
我有一个实现IDataErrorInfo的ViewModel和一个master-detail-view.当用户点击详细视图中的保存按钮而不是更早时,如何触发当前ViewModel项的变量?
我正在尝试使用IDataErrorInfo验证我的MVVM应用程序中的数据,但我遇到了一些问题.
当我使用无效值设置我的TextBox时,验证工作正常.但是在我将TextBox的值设置为有效值后,我得到了以下异常:
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
System.Windows.Data Error: 16 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0).[0].ErrorContent; DataItem='TextBox' (Name='txtRunAfter'); target element is 'TextBox' (Name='txtRunAfter'); target property is 'ToolTip' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter …Run Code Online (Sandbox Code Playgroud) 我正在使用MVVM架构开发WPF应用程序.我是WPF的业余爱好者所以请耐心等待..
我有两个模型类.父类具有另一个(子)类的对象作为其属性.(我的意思是嵌套对象而不是继承对象)
例如,请考虑以下方案.
public class Company
{
public string CompanyName {get; set;}
public Employee EmployeeObj {get; set;}
}
public class Employee
{
public string FirstName {get; set;}
public string LastName {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我想使用Enterprise Library Validation Block验证Employee实体的属性.
我可以通过在employee类中实现IDataErroInfo接口来实现,如下所示
public class Employee : IDataErrorInfo
{
[NotNullValidator(MessageTemplate="First Name is mandatory"]
public string FirstName {get; set;}
[StringLengthValidator(0,20,MessageTemplate="Invalid")]
public string LastName {get; set;}
public string Error
{
get
{
StringBuilder error = new StringBuilder();
ValidationResults results = Validation.ValidateFromAttributes<Employee>(this);
foreach (ValidationResult result in results)
{ …Run Code Online (Sandbox Code Playgroud) 我正在为WPF应用程序编写(尝试编写)单元测试.
UI绑定的业务对象实现IDataErrorInfo,这样当我在View xaml中设置ValidatesOnDataErrors = True时,只要调用绑定业务对象的setter,就会调用错误索引器(this []).那部分很棒.
现在,如果我从unitTest调用同一属性的setter,它永远不会调用错误索引器.如何强制在单元测试中评估IDataErrorInfo索引器?
仅用于说明,这是我的一个包含Name属性的简单错误索引器.设置'myObject.Name = string.Empty;' 当我在单元测试中执行此操作时,会调用setter,但不会调用错误索引器.
public string Name
{
get { return _name; }
set
{
_name = value;
IsDirty = true;
OnPropertyChanged("Name");
}
}
#region IDataErrorInfo
public Dictionary<string, string> ErrorCollection;
public string this[string property]
{
get
{
string msg = null;
switch (property)
{
case "Name":
if (string.IsNullOrEmpty(Name))
msg = "ICU Name is required.";
else if (Name.Length < 4)
msg = "ICU Name must contain at least 4 characters.";
else if (_parent.Units.AsEnumerable().Count(u => …Run Code Online (Sandbox Code Playgroud) 我正在使用Prism MVVM框架在WPF中实现数据验证.我在ViewModel中使用干净的数据实体,这些实体被绑定到表示层.
<TextBox Text="{Binding User.Email, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
我在基础ViewModel类中实现了IDataErrorInfo的通用实现,该类对我的Entity上的DataAnnotation属性运行验证(在本例中为User).
问题是当绑定到实体时,WPF框架在实体上查找IDataErrorInfo而不是ViewModel,这是我希望这个逻辑存在的地方.如果我用我的ViewModel中的属性包装我的实体,那么一切正常,但我不希望在ViewModel中使用实体.
有没有办法告诉WPF在ViewModel中查找IDataErrorInfo而不是绑定的子对象?
谢谢,迈克
介绍
我TextBox在视图中有两个,每个绑定到我的视图模型(Property1,Property2)中的一些属性.
TextBox或者在某些布尔值和属性上启用,IDataErrorInfo在视图模型中使用视图中的某些样式进行验证.
问题
我希望禁用项目时禁用验证样式.
NB1:目前,我发现的解决方案是直接在视图模型中更改验证方案,但这需要通知属性更改以强制视图重新读取IDataErrorInfo(虽然属性没有真正改变,只有选择器.. .)
NB2:我的问题是非常接近此一个,但描述和解决方案都太复杂,对我来说,真的明白了吧.
伪代码
<UserControl
<UserControl.Resources>
<Style TargetType="{x:Type Control}" x:Key="ControlValidationStyle">
...
</Style>
</UserControl.Resources>
...
<TextBox
Text="{Binding Property1,
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding IsMode1}"
Style="{StaticResource ControlValidationStyle}"
/>
<TextBox
Text="{Binding Property2,
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding IsMode1,
Converter={StaticResource BoolInverse}}"
Style="{StaticResource ControlValidationStyle}"
/>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
ControlValidationStyle
<Style TargetType="{x:Type Control}" x:Key="ControlValidationStyle">
<Style.Resources>
<Style TargetType="ToolTip">
<Setter Property="Background" Value="Tomato" />
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Foreground" Value="white" />
</Style> …Run Code Online (Sandbox Code Playgroud) 方法声明中的方括号如何与c#一致?这是我在阅读WPF验证时看到的,可以使用IDataErrorInfo以及以下示例.
public string this [string propertyName]
// Error handling takes place here.
public string this[string propertyName] // <== IE HERE
{
get
// etc
}
}
Run Code Online (Sandbox Code Playgroud)
我注意到MSDN说"方括号([])用于数组,索引器和属性.它们也可以用于指针." 那么上面的用法指针是什么?
在我的ViewModel中,我实现了IDataErrorInfo接口(以及INotifyPropertyChanged)。输入验证按预期工作,我在那里没有问题。
我将此属性作为IDataErrorInfo的一部分,public string Error { get { return this[null]; } }据我了解,Error如果所有已验证的输入均通过验证,则应为空,因此我将此作为我的CanExecute方法传递
return !string.IsNullOrEmpty(Error);
Run Code Online (Sandbox Code Playgroud)
但是,我的“保存”按钮从未启用。我的CanExecuteChanged猜想是永远不会被触发。如果是这样,我应该在哪里以及如何触发它?
这是我的RelayCommand类。我尝试了其他实现方式,但结果相同。我认为它有效,因为如果不将CanExecute方法传递给构造函数,则会启用“保存”按钮。
public class RelayCommand : ICommand
{
private readonly Action execute;
private readonly Func<bool> canExecute;
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
} …Run Code Online (Sandbox Code Playgroud) idataerrorinfo ×10
wpf ×7
mvvm ×6
validation ×5
c# ×3
xaml ×2
binding ×1
icommand ×1
prism ×1
relaycommand ×1
templates ×1
unit-testing ×1