我在过去的应用程序中使用了WPF和IDataErrorInfo,通过在装饰器中放置图像并向图像添加工具提示,通过controltemplate向用户显示错误;
<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,显示图像和工具提示;
<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工具提示,没有任何问题。问题是我似乎无法在Adorner中显示图像(或其他任何内容)。图像未显示,边框未更改。
以前的应用程序之间的区别在于视图位于ContentControl中的Region中,我使用依赖注入将viewmodel注入到视图构造函数中并设置DataContext。
我无法弄清楚为什么以前这样做不起作用。我想想我可能需要在某个地方加入AdornerDecorator,但我很困惑,因为在一些地方试过它没有成功。有关我如何确保装饰者出现的任何想法?
答案 0 :(得分:2)
使用AdornerDecorator来包装包含texbox的元素,并且一切正常。