具有验证的文本框在选项卡更改时丢失ErrorTemplate [英] TextBox with validation loses ErrorTemplate on tab change

查看:116
本文介绍了具有验证的文本框在选项卡更改时丢失ErrorTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TabControl的选项卡上有一个带有验证规则的TextBox.验证规则失败时,默认的ErrorTemplate会正确显示(TextBox周围的红色边框).
但是,如果切换到另一个选项卡,然后再返回到带有TextBox的选项卡,则ErrorTemplate高亮指示灯将消失.如果TextBox中发生更改,则仍将调用验证规则,并返回false,但仍不会显示错误突出显示.
只有当文本内容更改为有效然后又变为无效时,highligh才会卷土重来.
我想,如果文本内容无效,则切换到另一个选项卡并返回将保留无效的突出显示.任何欢迎获得这种行为的想法.
xaml:

I have a TextBox with a validation rule that is on a tab of a TabControl. The default ErrorTemplate correctly shows (red border around TextBox) when the validation rule fails.
However if there is a switch to another tab and then back to the tab with the TextBox the ErrorTemplate hightlight is gone. If there is a change in the TextBox the validation rule is still called and returns false but the error highlight still doesn't show.
Only when the text content is changed to be valid and then again to be invalid does the highligh comeback.
I would like that if the text content is invalid that switching to another tab and back keeps the invalid highlight. Any ideas to get this behaviour most welcome.
The xaml:

<TextBox Height="35" >
  <TextBox.Text>
    <Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <ps:PanIdValidation />
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

推荐答案

TabItem应该定义如下:

TabItem should be defined as follows:

<TabItem Header="Foo">
    <Border>
        <AdornerDecorator>
            <Grid>
                <TextBox Height="35" >
                    <TextBox.Text>
                         <Binding Path="pan_id" UpdateSourceTrigger="PropertyChanged">
                             <Binding.ValidationRules>
                                 <ps:PanIdValidation />
                             </Binding.ValidationRules>
                          </Binding>
                      </TextBox.Text>
                  </TextBox>
              </Grid>
          </AdornerDecorator>
      </Border>
  </TabItem>

问题是Validor.Error提示被绘制在Adorner层中.切换标签时,该层将被丢弃.

The issue is, the Validation.Error cues are painted in the Adorner Layer. When you switch tabs, that layer is discarded.

这篇关于具有验证的文本框在选项卡更改时丢失ErrorTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆