WPF UserControl的ValidationRule触发了, 但是这个TextBox的默认的ErrorTemplate没有看到红框
我创建了一个UserControl工程,里面有个TextBox,绑定了一个ValidationRule, 然后我在另一个wpf窗体程序中插入了这个UserControl,调试发现Textbox的ValidationRule触发了,但是默认的红框没有出现。
如果我把UserControl基类改为Window, 单独ShowDialog, 红框是有的。
相关代码:
<UserControl 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" mc:Ignorable="d" x:Class="UserControlValidationDemo.UserControl1" xmlns:UserControlValidationDemo_ValidationRules="clr-namespace:UserControlValidationDemo.ValidationRules" xmlns:UserControlValidationDemo_ViewModels="clr-namespace:UserControlValidationDemo.ViewModels" x:Name="UserControl" d:DesignWidth="640" d:DesignHeight="480"> <UserControl.Resources> <UserControlValidationDemo_ViewModels:MainViewModel x:Key="MainViewModelDataSource" d:IsDataSource="True"/> </UserControl.Resources> <UserControl.DataContext> <Binding Mode="OneWay" Source="{StaticResource MainViewModelDataSource}"/> </UserControl.DataContext> <Grid x:Name="LayoutRoot" Background="White"> <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" x:Name="txtTest"> <TextBox.Text> <Binding Path="Collection[0].Property1" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <UserControlValidationDemo_ValidationRules:TestValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </Grid></UserControl>