如何在WPF中关闭默认验证?
WPF似乎有一些默认启用的验证规则。当我在绑定文本框中输入非数字文本并对其进行制表时,会在其周围显示一个读取边框。这是怎么回事?我已将ValidatesOnExceptions设置为false,验证规则从何而来?我使用的是.Net framework的4.5.2版 这是我的XAML如何在WPF中关闭默认验证?,wpf,Wpf,WPF似乎有一些默认启用的验证规则。当我在绑定文本框中输入非数字文本并对其进行制表时,会在其周围显示一个读取边框。这是怎么回事?我已将ValidatesOnExceptions设置为false,验证规则从何而来?我使用的是.Net framework的4.5.2版 这是我的XAML <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/x
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="159.206" Width="193.953">
<Grid>
<TextBox x:Name="textBox" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap"
Text="{Binding Foo, ValidatesOnExceptions=False, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Width="91" Margin="10,10,0,0"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="10,48,0,0"
TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="91"/>
</Grid>
</Window>
您不能将int
属性设置为有效的int
值以外的任何值
这种“验证”,或者更确切地说是编程语言的类型安全特性,不能关闭
但是,如果愿意,您可以删除或自定义默认错误模板。只需将Validation.ErrorTemplate
属性设置为emotyControlTemplate
:
<TextBox x:Name="textBox" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap"
Text="{Binding Foo, ValidatesOnExceptions=False, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Width="91" Margin="10,10,0,0">
<Validation.ErrorTemplate>
<ControlTemplate />
</Validation.ErrorTemplate>
</TextBox>
非常感谢。如果有一个int呢?我希望空白文本映射为null,默认情况下这似乎不起作用。我必须写一个自定义转换器吗?可能吧。您需要将空字符串转换为null。String.Empty不是有效的int?价值
<TextBox x:Name="textBox" Height="23" HorizontalAlignment="Left" VerticalAlignment="Top" TextWrapping="Wrap"
Text="{Binding Foo, ValidatesOnExceptions=False, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
Width="91" Margin="10,10,0,0">
<Validation.ErrorTemplate>
<ControlTemplate />
</Validation.ErrorTemplate>
</TextBox>