问题描述
在验证失败时,TextBox或ComboBox不会被redborder高亮显示.
TextBox or ComboBox is not highlited with redborder on validation failure.
我有如下所示的itemscontrol,根据条件有3个数据模板.
I have itemscontrol like below, with 3 datatemplates based on condition.
我已经为验证规则定义了正则表达式模式.
<边界Grid.Row ="1"; Style ="{StaticResource BorderTop}">
< ScrollViewer x:Name =" sv" MaxHeight ="80". VerticalScrollBarVisibility =自动">
< StackPanel x:Name =" customControlsStack">
< ItemsControl x:Name ="itemsControl";已加载="ItemsControl_OnLoaded">
< ItemsControl.ItemsPanel>
< ItemsPanelTemplate>
<!- UniformGrid将在每行中为所有屏幕分辨率安排3个自定义字段. ->
< UniformGrid HorizontalAlignment =拉伸";列="3". />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
< ItemsControl.ItemTemplate>
< DataTemplate
< ContentControl Content =" {Binding}">
< ContentControl.Style>
< Style TargetType =" ContentControl">
< Style.Triggers>
< DataTrigger Binding =" {Binding EditStyle}"值=编辑">
< Setter Property =" ContentTemplate"值="{StaticResource txtCustomFieldDataTemplate}" />
</DataTrigger>
< DataTrigger Binding =" {Binding EditStyle}"值="DropDownOnly">
< Setter Property =" ContentTemplate"值="{StaticResource comboBoxCustomReadOnlyFieldDataTemplate}" />
</DataTrigger>
< DataTrigger Binding =" {Binding EditStyle}"值="DropDown">
< Setter Property =" ContentTemplate"值="{StaticResource comboBoxCustomFieldDataTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</Border>
I have defined regularexpression pattern to my validation rule.
<Border Grid.Row="1" Style="{StaticResource BorderTop}">
<ScrollViewer x:Name="sv" MaxHeight="80" VerticalScrollBarVisibility="Auto">
<StackPanel x:Name="customControlsStack">
<ItemsControl x:Name="itemsControl" Loaded="ItemsControl_OnLoaded">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!-- UniformGrid will arrange 3 custom fields in each row for all screen resolutions -->
<UniformGrid HorizontalAlignment="Stretch" Columns="3" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Style.Triggers>
<DataTrigger Binding="{Binding EditStyle}" Value="Edit">
<Setter Property="ContentTemplate" Value="{StaticResource txtCustomFieldDataTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding EditStyle}" Value="DropDownOnly">
<Setter Property="ContentTemplate" Value="{StaticResource comboBoxCustomReadOnlyFieldDataTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding EditStyle}" Value="DropDown">
<Setter Property="ContentTemplate" Value="{StaticResource comboBoxCustomFieldDataTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
</Border>
我的3个数据模板是:文本框,组合框,可编辑的组合框
My 3 Data Templates are: Textbox, combobox, editable combobox
< DataTemplate x:Key =" txtCustomFieldDataTemplate">
< StackPanel Margin ="3,1,3,3";方向=垂直">
< Label HorizontalAlignment =居中"
Content =""{Binding Path = Caption}"
Style =" {DynamicResource SmallNormalTextStyle}" />
< TextBox x:Name =" txtCustomField"
MinWidth ="150"
HorizontalAlignment =拉伸"
AllowDrop =假"
GotFocus ="Field_GotFocus"
IsEnabled ="{Binding AreCorrectionFieldsEnabled}"
IsVisibleChanged =" Field_IsVisibleChanged"
MaxLength ="{Binding Path = Length}"
Style =" {DynamicResource SmallNormalTextStyle}"
TabIndex ="32"
Tag =" {Binding Path = RegexPattern}"
TextAlignment ="{{Binding Path = Alignment}""
TextChanged =" Field_TextChanged"
TextWrapping ="NoWrap">
< TextBox.Text>
< Binding Path ="EnvVarValue"; UpdateSourceTrigger ="PropertyChanged">
< Binding.ValidationRules>
< local:RegExValidator RegexPattern ="RegexPattern"; ValidationStep ="UpdatedValue". />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="txtCustomFieldDataTemplate">
<StackPanel Margin="3, 1, 3, 3" Orientation="Vertical">
<Label HorizontalAlignment="Center"
Content="{Binding Path=Caption}"
Style="{DynamicResource SmallNormalTextStyle}" />
<TextBox x:Name="txtCustomField"
MinWidth="150"
HorizontalAlignment="Stretch"
AllowDrop="False"
GotFocus="Field_GotFocus"
IsEnabled="{Binding AreCorrectionFieldsEnabled}"
IsVisibleChanged="Field_IsVisibleChanged"
MaxLength="{Binding Path=Length}"
Style="{DynamicResource SmallNormalTextStyle}"
TabIndex="32"
Tag="{Binding Path=RegexPattern}"
TextAlignment="{Binding Path=Alignment}"
TextChanged="Field_TextChanged"
TextWrapping="NoWrap">
<TextBox.Text>
<Binding Path="EnvVarValue" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:RegExValidator RegexPattern="RegexPattern" ValidationStep="UpdatedValue" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
</DataTemplate>
推荐答案
.
您在那里设置了很多属性,并且进行了各种各样的设计或样式设置.现在,您意识到您的验证没有显示.
You have an awful lot of properties set there and all sorts or styling going on. Now you realise your validation isn't showing.
与其他线程中的症状相同,并且可能具有相同的根本问题.
Same symptoms as you have in your other threads and probably with the same root problem.
走错路了.
您应该先使基础知识起作用,然后再证明其样式.
You should get the basics working first, prove them and then style.
另一个问题是,没有人可以复制您正在做的事情.
Another problem is that nobody can reproduce what you're doing.
如果我拿出所有这些依赖项并编写自己的验证器,然后将自己的正则表达式放在一起,那我敢打赌,这对我来说将是成功的.
If I took out all those dependencies and wrote my own validator and put my own regex together than I bet it'd just work for me.
.
在文本框中添加该标签,以查看是否会在工具提示中出现错误:
Stick this in with your textbox and see if you can get an error in a tooltip or not:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
如果它显示出来,那么您在该文本框上插入了模板,因此没有错误模板.
If it shows up then you junked your template on that textbox so there's no error template.
如果未显示,则您的验证程序无法正常工作.
If it doesn't show up then your validator isn't working properly.
.
没有足够的线索或不切实际?
Not enough clues or not practical?
.
开始一个新的解决方案,仅使用一个文本框,它是绑定对象的一个实例,只需添加您的验证规则即可.
Start a new solution take just a textbox, an instance of whatever that is bound to and add in JUST your validationrule.
这肯定可以在常规文本框中使用吗,还是不起作用?
Does that definitely work on a regular textbox, or does it not work?
以任何一种方式证明它.
Prove it either way.
如果它可以正常工作,那么您就会知道该样式中的某个位置(或默认样式应用于某个位置)已经丢失了错误模板.
If it works then you know somewhere in that styling (or the default style applied somewhere) you've lost the error template.
开始将位添加到您的新解决方案中,看看它何时中断.
Start adding bits into your new solution and see when it breaks.
这篇关于TextBox或Editable ComboboxvalidationRule在ItemsControl中没有带有红色边框的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!