首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >访问ValidationResult

访问ValidationResult
EN

Stack Overflow用户
提问于 2018-04-11 17:12:40
回答 1查看 375关注 0票数 2

嗨,我有一个数据网格与文本框接收作为输入的ip地址。为了验证文本框,我将其绑定到我的自定义验证器。

代码语言:javascript
运行
复制
<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,或者更好的是,将它与我的视图模型绑定?

EN

回答 1

Stack Overflow用户

发布于 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

我设置了

代码语言:javascript
运行
复制
NotifyOnSourceUpdated=True,
NotifyOnValidationError=True,

在我感兴趣的所有绑定上。然后错误就会浮现出来。它们被捕获并从资源字典中的标准模板传递到视图模型。ConversionErrorCommand将触发转换,验证结果失败。

代码语言:javascript
运行
复制
    <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:

代码语言:javascript
运行
复制
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;
    }
}

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49771072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档