我创建了一个样式,以便指定在我的某些文本框中存在验证错误时要使用的验证ControlTemplate。验证错误被捕获,因为我可以看到我的样式被使用(文本框的默认红色边框,以及我添加的浅粉红色背景,红色" !!!"字符串),但问题是" !!!"红色字符串始终存在,即使没有验证错误(尽管粉红色背景和红色边框消失)。我正在使用IDataErrorInfo来验证文本框。
这是我的xaml样式代码:
<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="LightPink"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<AdornedElementPlaceholder x:Name="ControlWithError"/>
<TextBlock DockPanel.Dock="Right"
Foreground="Red"
FontSize="12pt"
FontWeight="Bold"
Margin="-18,0,0,0"
Text="!!!">
</TextBlock>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我尝试将此行添加到&lt; TextBlock&gt;节点,但它没有工作:
Visibility="{Binding Validation.HasError, Source={RelativeSource Self}, Converter={StaticResource BoolToHiddenOrVisibleConverter}}"
我的问题是:如何制作&#34; !!!&#34;只有当Validation.HasError标志设置为true时才会出现红色字符串?
以下是我如何宣布我的一个文本框作为参考。 OneTextBox是一个封装常规WPF TextBox的控件,并添加了一些功能(因为我使用的是自定义框架):
<pres:OneTextBox Grid.Row="0" Watermark="Name..." Margin="85,12,0,0" Style="{StaticResource ErrorValidationTextBox}"
Text="{Binding Path=InterfaceSpecification.Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
AcceptsReturn="False" MaxLines="1" Height="22" VerticalAlignment="Top"
HorizontalAlignment="Left" Width="300" />
编辑:我在调试中运行时遇到以下绑定错误:
BindingExpression path error: 'Validation' property not found on 'object' ''RelativeSource' (HashCode=58276509)'. BindingExpression:Path=Validation.HasError; DataItem='RelativeSource' (HashCode=58276509); target element is 'TextBlock' (Name=''); target property is 'Visibility' (type 'Visibility')
EDIT2:以下是我在班级中实现IDataErrorInfo的方法:
public string Error
{
get { return mError; }
set { mError = value; }
}
public string this[string columnName]
{
get
{
switch (columnName)
{
case "Name":
if (string.IsNullOrWhiteSpace(Name))
{
Error = "The name cannot be null, empty or contain only white spaces";
}
else if (Name.StartsWith(" "))
{
Error = "The name cannot start with a white spaces";
}
else if (Name.IndexOfAny(Path.GetInvalidPathChars()) != -1)
{
Error = "The name cannot contain invalid characters";
}
else
{
Error = null;
}
break;
}
return Error;
}
}
答案 0 :(得分:1)
这肯定有效
<Style x:Key="ErrorValidationTextBox" TargetType="{x:Type pres:OneTextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="LightPink"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<AdornedElementPlaceholder x:Name="ControlWithError"/>
<TextBlock DockPanel.Dock="Right"
Foreground="Red"
FontSize="12pt"
FontWeight="Bold"
Margin="-18,0,0,0"
Text="!!!"
Visibility="{Binding ElementName=ControlWithError, Path=AdornedElement.(Validation.HasError),Converter={StaticResource BoolToHiddenOrVisibleConverter}}" >
</TextBlock>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>