J4N*_*J4N 5 validation wpf styles idataerrorinfo
我有一个用户控件来验证它的内容。
我正在使用 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>
Run Code Online (Sandbox Code Playgroud)
问题是,在某些情况下,我必须隐藏表单(当没有选择任何元素时),但是当我显示的表单没有错误时,然后我折叠表单(网格),UserControls 中的文本框(这是无效的,因为它们不接受空值)得到红色边框和星号:

未隐藏时的相同形式:
请注意:只有 ErrorTemplate 的内容是可见的,触发器的内容(粉红色背景,黑色前景)不适用。
所以我认为这种风格有问题,但我对 WPF 风格不够熟悉,不知道为什么。
另一个奇怪的事情:如果我有具有相同验证的文本框(而不是用户控件中的文本框),它们会被正确隐藏。
编辑 我发现了一些对我有很大帮助的其他东西,首先,这个主题: 隐藏控件时隐藏验证装饰 有了这个,我做了以下事情:将我的用户控件可见性绑定到隐藏元素可见性,然后在用户控件中,我将文本框可见性绑定到用户控件可见性,然后(最后),我添加一个样式触发器:
<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>
Run Code Online (Sandbox Code Playgroud)
它几乎有效!,唯一剩下的就是一个小红点,它无处不在

知道它来自哪里吗?
我找到了一个解决方法,这可能不是最好的方法(如果你有更好的方法,请告诉我!)。
因此,首先,当我使用用户控件时,我将其可见性绑定到我要隐藏的用户控件:
<userContols:BrowseFileControl Visibility="{Binding ElementName=uxFormGrid, Path=Visibility}"/>
Run Code Online (Sandbox Code Playgroud)
其次,在 IDataErrorInfo 方法(public string this[string columnName])中,如果显示当前控件,我仅返回错误。
public string this[string columnName]
{
get
{
String result=null;
if (Visibility == Visibility.Visible)
{
if (columnName == "FilePath")
{
if (String.IsNullOrEmpty(FilePath))
{
if (!CanBeEmpty)
{
result = "Mandatory field";
}
}
else if (!IsValidFilePath(FilePath))
{
result = "Malformed path";
}
}
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢处理样式,但我没有找到完全去除任何红色标记的方法。
| 归档时间: |
|
| 查看次数: |
1130 次 |
| 最近记录: |