我一直在尽一切努力解决这个问题。:(
对于文本框,我有一个 Validation.ErrorTemplate 设置,文本框右侧有一个图像,但它有一个验证错误。
这很好用!但我想做的一件事是调整有错误的文本框的大小或设置边距,使其适合文本框在表单上的空间。目前,图像在文本框区域之外流动。
我真正想要的是有错误的文本框占用与没有文本框相同的空间。
这是我的 XAML 风格:
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<my:TextBoxWidthTransformConverter x:Key="TextBoxWidthTransformConverter"/>
</Style.Resources>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Margin" Value="{Binding Converter={StaticResource TextBoxWidthTransformConverter}, RelativeSource={RelativeSource Self}}"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel
Margin="{TemplateBinding Margin}"
Orientation="Horizontal"
RenderOptions.BitmapScalingMode="NearestNeighbor"
>
<AdornedElementPlaceholder
Grid.Column="1"
Grid.Row="1"
Name="controlWithError"
/>
<Border Width="2"/>
<Image
ToolTip="{Binding ElementName=controlWithError, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"
Source="imagepath"
/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
我正在使用转换器 TextBoxWidthTransformConverter 只是为了看看我是否可以发生一些事情,但在我只是在值中使用“0,0,20,0”之前,无济于事。转换器不会触发,保证金不会改变。我使用 Snoop 来查看是否可以看到被触摸或更改的属性,但没有任何反应。
Margin 是 Validation.HasError 属性无法更改的属性吗?
任何见解都会很棒!
谢谢!