【发布时间】:2012-11-29 13:27:32
【问题描述】:
我有一个用户控件来验证它的内容。
我正在使用 IDataErrorInfo 来验证输入(我必须使用 .Net 3.5)。
我正在学习本教程: http://japikse.blogspot.ch/2009/07/idataerrorinfo-error-templates-and-wpf.html
这意味着我正在使用以下样式:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True"
ToolTip="{Binding ElementName=controlWithError,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock DockPanel.Dock="Right"
Foreground="Red"
FontSize="14pt"
Margin="-15,0,0,0" FontWeight="Bold">*
</TextBlock>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder Name="controlWithError" />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
问题是,在某些情况下,我必须隐藏表单(当没有选择任何元素时),但是当我显示的表单没有错误时,然后我折叠表单(网格), UserControls 中的文本框(无效,因为它们不接受空值)有红色边框和星号:
不隐藏时的相同形式: 请注意: 只有ErrorTemplate的内容是可见的,触发器的内容(粉红色背景,黑色前景)不适用。
所以我认为这种风格有问题,但我对 WPF 风格不够熟悉,无法理解原因。
另外一件奇怪的事情: 如果我有具有相同验证的文本框(而不是用户控件中的文本框),它们会被正确隐藏。
编辑 我发现了一些对我有很大帮助的其他东西,首先是这个主题: Hiding validation adornment when hiding a control 有了这个,我做了以下事情:将我的用户控件可见性绑定到隐藏元素可见性,然后在用户控件中,我将文本框可见性绑定到用户控件可见性,然后(最后)添加一个样式触发器:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="Pink"/>
<Setter Property="Foreground" Value="Black"/>
</Trigger>
<Trigger Property="Visibility" Value="Visible">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True" ToolTip="{Binding ElementName=controlWithError,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
<TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14pt" Margin="-15,0,0,0" FontWeight="Bold">*</TextBlock>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder Name="controlWithError" />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
几乎可以用了!,唯一剩下的就是一个小红点,不知从何而来
有什么想法吗?
【问题讨论】:
标签: wpf styles idataerrorinfo validation