【问题标题】:WPF IDataErrorInfo issuesWPF IDataErrorInfo 问题
【发布时间】:2013-01-17 09:54:29
【问题描述】:

我在过去的应用程序中使用 WPF 和 IDataErrorInfo 通过控件模板向用户显示错误,方法是在装饰器中放置图像并像这样向图像添加工具提示;

<Style x:Key="textStyle" TargetType="TextBox">
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <DockPanel LastChildFill="True">
          <Border BorderBrush="Orange"
                  BorderThickness="2"
                  CornerRadius="4"
                  SnapsToDevicePixels="True">
            <Border.Effect>
              <DropShadowEffect BlurRadius="10"
                                ShadowDepth="0"
                                Color="Orange" />
            </Border.Effect>
            <DockPanel>
              <Image Width="16"
                     Height="16"
                     Margin="-20,0,0,0"
                     HorizontalAlignment="Left"
                     VerticalAlignment="Center"
                     RenderOptions.BitmapScalingMode="HighQuality"
                     Source="{StaticResource imgError}"
                     ToolTip="{Binding ElementName=adornedElement,
                                       Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"
                     ToolTipService.ShowDuration="30000" />
              <AdornedElementPlaceholder Name="adornedElement" />
            </DockPanel>
          </Border>
        </DockPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

通过在 ViewModel 中适当实现 IDataErrorInfo 并在视图中相应地设置文本框,将显示图像和工具提示;

<TextBox Name="txt"
         Grid.Column="0"
         Height="40"
         Background="Aqua"
         Style="{StaticResource textStyle}"
         Text="{Binding Path=Text,
                        UpdateSourceTrigger=PropertyChanged,
                        ValidatesOnDataErrors=True}" />
<TextBlock Grid.Column="1"
           Height="40"
           Background="AliceBlue"
           Text="{Binding ElementName=txt,
                          Path=(Validation.Errors).CurrentItem.ErrorContent}" />

以上代码在我以前的应用程序中正确显示,并在文本块确认的图像工具提示中显示错误。

但是,在我当前使用 Prism 构建的应用程序中,我无法显示图像。 TextBlock 正确更新,我可以通过样式触发器将错误设置为 TextBox 工具提示,而不会出现任何问题。问题是我似乎无法让图像(或其他任何东西)显示在装饰器中。图像未显示,边框未更改。

与以前的应用程序之间的区别在于视图位于 ContentControl 的区域中,我使用依赖注入将视图模型注入视图构造函数并设置 DataContext。

我无法弄清楚为什么以前这样做时这不起作用。我认为我可能需要在某处包含一个 AdornerDecorator,但我对在几个地方尝试过但没有成功的地方感到困惑。有什么想法可以确保展示 Adorner?

【问题讨论】:

  • 我是对的 - 这是缺少 AdornerDecorator 的要求。我必须将文本框或其容器包装在 AdornerDecorator 标记中,并且图像等显示正确

标签: wpf prism-4 idataerrorinfo adornerdecorator


【解决方案1】:

使用 AdornerDecorator 包装包含 texbox 的元素,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多