我的WPF应用程序中有一个TextBox.我已经为验证错误定义了一个ControlTemplate,如下所示:
<ControlTemplate x:Key="validationTemplate">
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Bottom" Text="Invalid Input: "></TextBlock>
<AdornedElementPlaceholder />
</DockPanel>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)
我的TextBox如下:
<TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}">
<TextBox.Text>
<Binding Path="TEXT1" ValidatesOnDataErrors="True" validatesOnExceptions="True">
</Binding>
</TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
现在,如果我的TextBox添加了ValidationRule,然后我在那里验证,错误模板正确应用.但由于其他一些问题我无法做到这一点.
所以我必须在PreviewLostKeyboardFocus中验证TextBox的内容.我正在验证TextBox.现在我想在后面的代码中为TextBox设置错误模板,但我无法做到!!
我尝试了这个,但它并不像有意的那样工作::
private void blockTextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox txtBox = sender as TextBox;
txtBox.Template = this.FindResource("validationTemplate") as ControlTemplate;
//this behaves strange; it removes the TextBox and places the ErrorTemplate.
//I want it to behave like the way WPF does internally wherein it places
//the error template around TExtBox
}
Run Code Online (Sandbox Code Playgroud)
问题1:我想知道如何将错误模板添加到TextBox
问题2:我想知道如何从代码中设置控件模板的错误消息.例如,我想将默认错误消息"Invalid …
所以我有一个像这个简化版本的控件:
<local:ImageMapField x:Class="ImageApp.WPF.Controls.ImageMapContentField"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ImageApp.WPF.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="Me">
<Grid HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" HorizontalAlignment="Stretch" Style="{DynamicResource BaseLabelStyle}">
<TextBlock Text="{Binding Header, RelativeSource={RelativeSource AncestorType=local:ImageMapContentField, Mode=FindAncestor}}" TextWrapping="WrapWithOverflow"></TextBlock>
</Label>
<StackPanel Grid.Column="1">
<Image />
<Border Margin="20,5,5,2">
<ContentPresenter Content="{Binding DataEntryContent, ElementName=Me}" />
</Border>
</StackPanel>
</Grid>
</local:ImageMapField>
Run Code Online (Sandbox Code Playgroud)
我像这样使用它:
<controls:ImageMapContentField Header="Foo Date"
FieldName="FooDate"
ImageSource="{Binding MyImage, Mode=TwoWay}"
ItemsSource="{Binding Map.Items}"
Zoom="{Binding MapFieldZoom}">
<controls:ImageMapContentField.DataEntryContent>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding MyDate, StringFormat=MM/dd/yyyy, ValidatesOnDataErrors=True, NotifyOnValidationError=True}">
<controls:WatermarkService.Watermark>
<TextBlock>Date</TextBlock>
</controls:WatermarkService.Watermark> …Run Code Online (Sandbox Code Playgroud)