我一直在试图解决这个问题。 :(WPF Validation.HasError设置文本框的边距
对于一个文本框,我有一个Validation.ErrorTemplate设置与文本框的右侧的图像,同时它有一个验证错误。
这个伟大的工程!但有一两件事我想这样做是调整或设置边距的文本框有错误,因此它适合在窗体上的文本框的空间。目前,图像流出的文本框区域之外。
我真正想要的是带有错误的文本框占据与textbox相同的空间。
这是我的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属性进行更改的吗?
任何洞见将是美好的!
谢谢!