Wpf ControlTemplate和validation-如何定位项目?
我创建了ControlTemplate,如果我的文本框中存在验证错误,就会显示该模板。我的controltemplate看起来像那样Wpf ControlTemplate和validation-如何定位项目?,wpf,validation,xaml,controltemplate,controltemplates,Wpf,Validation,Xaml,Controltemplate,Controltemplates,我创建了ControlTemplate,如果我的文本框中存在验证错误,就会显示该模板。我的controltemplate看起来像那样 <ControlTemplate x:Key="TextBoxErrorTemplate"> <TextBlock Foreground="Orange" FontSize="12pt">Field can't be empty</TextBlock> </ControlTemplate> 字段不能为空
<ControlTemplate x:Key="TextBoxErrorTemplate">
<TextBlock Foreground="Orange" FontSize="12pt">Field can't be empty</TextBlock>
</ControlTemplate>
字段不能为空
但是,如果发生验证错误,textBox上会出现textBlock,并且用户无法输入正确的值。有没有办法设置TextBlock的位置-显示错误信息的位置?错误模板用于修饰控件,而不是更改其内部属性,为此,您应使用带有相应触发器的样式:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Foreground" Value="Orange"/>
<Setter Property="FontSize" Value="12"/>
</Trigger>
</Style.Triggers>
</Style>
如果要显示某些文本,可以使用如下模板:
<ControlTemplate x:Key="TextBoxErrorTemplate">
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder/>
<TextBlock Foreground="Orange" FontSize="12pt">Field can't be empty</TextBlock>
</StackPanel>
</ControlTemplate>
字段不能为空
文本块将显示在文本框的右侧
如果您只是想显示错误消息,我建议您设置文本框的工具提示,并将其绑定到验证错误。您能解释一下这个标记吗?没有开始标记,似乎您正在关闭这个标记