嗨,我有一个数据网格与文本框接收作为输入的ip地址。为了验证文本框,我将其绑定到我的自定义验证器。
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Margin="20,10,20,10" Height="20" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="{Binding Path=IPSrcValidationStatus.Color}">
<TextBox.Text>
<Binding Path="IPSrc" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:IPv4ValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>我如何从我的代码访问ValidationResult,或者更好的是,将它与我的视图模型绑定?
发布于 2018-04-11 17:40:15
Validationrules完全在UI中发生。与转换失败类似,它们会将您的控件设置为有错误,但不会发生从视图到视图模型的数据传输。
与wpf通常的情况一样,有几种方法可以告诉视图模型视图中有数据错误。
您的验证规则可以获取绑定表达式并在视图模型上设置一些属性。在Maxence的帖子中有一些代码:Passing state of WPF ValidationRule to View Model in MVVM
我从来没有使用过这种方法(但它看起来会起作用)。
我通常想知道转换失败以及任何验证规则失败。在使用验证规则的场景中,我通常只关心有效数据是否进入视图模型和IsDirty==true
我经常使用的方法是在父网格中的控件树上出现错误时获取错误。这里的示例包含我使用的代码:
https://gallery.technet.microsoft.com/scriptcenter/WPF-Entity-Framework-MVVM-78cdc204
我设置了
NotifyOnSourceUpdated=True,
NotifyOnValidationError=True,在我感兴趣的所有绑定上。然后错误就会浮现出来。它们被捕获并从资源字典中的标准模板传递到视图模型。ConversionErrorCommand将触发转换,验证结果失败。
<Grid ... >
<i:Interaction.Triggers>
<local:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
<e2c:EventToCommand
Command="{Binding EditVM.TheEntity.ConversionErrorCommand, Mode=OneWay}"
EventArgsConverter="{StaticResource BindingErrorEventArgsConverter}"
PassEventArgsToCommand="True" />
</local:RoutedEventTrigger>
<local:RoutedEventTrigger RoutedEvent="{x:Static Binding.SourceUpdatedEvent}">
<e2c:EventToCommand
Command="{Binding EditVM.TheEntity.SourceUpdatedCommand, Mode=OneWay}"
EventArgsConverter="{StaticResource BindingSourcePropertyConverter}"
PassEventArgsToCommand="True" />
</local:RoutedEventTrigger>
</i:Interaction.Triggers>您还需要RoutedEventTrigger:
public class RoutedEventTrigger : EventTriggerBase<DependencyObject>
{
RoutedEvent routedEvent;
public RoutedEvent RoutedEvent
{
get
{
return routedEvent;
}
set
{
routedEvent = value;
}
}
public RoutedEventTrigger()
{
}
protected override void OnAttached()
{
Behavior behavior = base.AssociatedObject as Behavior;
FrameworkElement associatedElement = base.AssociatedObject as FrameworkElement;
if (behavior != null)
{
associatedElement = ((IAttachedObject)behavior).AssociatedObject as FrameworkElement;
}
if (associatedElement == null)
{
throw new ArgumentException("This only works with framework elements");
}
if (RoutedEvent != null)
{
associatedElement.AddHandler(RoutedEvent, new RoutedEventHandler(this.OnRoutedEvent));
}
}
void OnRoutedEvent(object sender, RoutedEventArgs args)
{
base.OnEvent(args);
}
protected override string GetEventName()
{
return RoutedEvent.Name;
}
}}
https://stackoverflow.com/questions/49771072
复制相似问题