【问题标题】:Accessing Validation.Errors from an ErrorTemplate从 ErrorTemplate 访问 Validation.Errors
【发布时间】:2013-05-26 16:27:59
【问题描述】:

我在网格中有一个 BindingGroup:

<Grid x:Name="участнКонтейн" DataContext="{Binding Source={StaticResource участнПк}}"
    Grid.RowSpan="1" Grid.Row="1" HorizontalAlignment="Center">
  <Grid.BindingGroup>
    <BindingGroup NotifyOnValidationError="True">
      <BindingGroup.ValidationRules>
        <цл:валидУчастн ValidationStep="ConvertedProposedValue" />
      </BindingGroup.ValidationRules>
    </BindingGroup>
  </Grid.BindingGroup>
  <Grid.Style>
    <Style>
      <Setter Property="Validation.ErrorTemplate" Value="{StaticResource BindingGroupШаблОш}" />
    </Style>
  </Grid.Style>
  ...

我的网格有一个 ErrorTemplate:

<ControlTemplate x:Key="BindingGroupШаблОш">
  <Border BorderBrush="Blue" BorderThickness="2">
    <StackPanel>
      <Label Content="My BindingGroup Error should be here!"></Label>
      <AdornedElementPlaceholder />
    </StackPanel>
  </Border>
</ControlTemplate>

我想从我的 ControlTemplate 访问 Validation.Errors[0].ErrorContent 以在我的标签中显示它。可能吗?你能帮帮我吗?

【问题讨论】:

  • +1 是一个很好但非常罕见的问题。我非常同意这个解决方案没有被广泛发布。我刚刚在 Validation.ErrorTemplate Attached Property 页面上向 MSDN 发布了一条建议,要求他们添加此 DataContext = Validation.Errors 信息,但我们只能等着看他们是否有任何作用。

标签: wpf validation xaml controltemplate errortemplate


【解决方案1】:

试试

<ControlTemplate x:Key="BindingGroupШаблОш">
  <Border BorderBrush="Blue" BorderThickness="2">
    <StackPanel>
      <Label Content="{Binding Path=[0].ErrorContent}"></Label>
      <AdornedElementPlaceholder />
    </StackPanel>
  </Border>
</ControlTemplate>

【讨论】:

  • 我花了很多时间阅读 MSDN,但我没有找到解决方案。这很容易。它写在哪里? Microsoft 认为我应该如何知道这一点?
  • 是的,这很奇怪。很久以前我也遇到过同样的问题,当(Validation.Errors)[0].ErrorContent 不起作用时,我只是尝试将TextBlockText 设置为DataContext,只是为了看看我读到的@987654326 是什么@ 在实际控件上方占据不同的可视层,因此 RelativeSource FindAncestor 无法正常工作。原来DataContextValidationError 类,因此试图将其从路径中删除并且它有效。我敢打赌它记录在某处关于 DataContext 或有人之前发现它:)
  • 我也是,我尝试显示除错误内容以外的其他内容。我尝试过 TemplateBinding、RelativeSource Self,尝试了不同的属性(例如名称、宽度),但没有任何显示。最后我找到了解决方法:我没有在 ErrorTemplate 中显示 ErrorContent,而是在主窗口中显示它,但是显示我的错误的 Label 的 Visibility 属性绑定到我验证的 BindingGroup 的 HasError 属性。所以当错误发生时,标签是Visible,否则Collapsed。这工作得很好,但它并不好而且“漂亮”。但是您的解决方案效果很好!谢谢。
  • +1 表示一个好的解决方案和一个找到解决方案的好方法。很遗憾我不能给你+2。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-04
  • 1970-01-01
  • 2015-01-08
相关资源
最近更新 更多