问题与WPF验证(IDataErrorInfo的)和制表聚焦 [英] Issue with WPF validation(IDataErrorInfo) and tab focusing

查看:182
本文介绍了问题与WPF验证(IDataErrorInfo的)和制表聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框绑定到一个对象,它实现了 IDataErrorInfo的的属性。
我设置了 Validation.ErrorTemplate 文本框,以及它工作正常。问题是,我有这些对的TabControl ,并验证模板不显示任何更多,如果我更改选项卡到另一个,然后再回到最初的标签(其中文本框是)。它看起来是验证(如值是正确的),但实际上它不是。

I have a TextBox bound to a property of an object which implements IDataErrorInfo. I set up the Validation.ErrorTemplate of the TextBox, and it works fine. The problem is that I have these on a TabControl, and the validation template doesn't display any more if I change the tab to another one and then come back to the initial tab (where the TextBox is). It looks like it is validated(like the value is correct), but actually it is not.

这是 IDataErrorInfo的对象 - 注意,是正确的值是2的长度的字符串:

This is the IDataErrorInfo object - note that a "correct" value is a string with a length of 2:

public class Presenter : IDataErrorInfo
{
    public Presenter()
    {
        this.Property = String.Empty;
    }

    public string Property { get; set; }

    public string Error { get { return null; } }

    public string this[string columnName]
    {
        get
        {
             if (columnName == "Property")
             {
                if (this.Property.Length == 2)
                   return null;
                else
                   return "Invalid property length!";
             }
             else return null;
        }
    }
}



这是XAML:

and this is the XAML:

<TabControl >
    <TabItem Header="tabItem1" Name="tabItem1" GotFocus="tabItem1_GotFocus">
        <Grid>
            <TextBox Width="100" Height="20" x:Name="txtField">
                <TextBox.Style>
                    <Style TargetType="{x:Type TextBox}">
                        <Setter Property="Validation.ErrorTemplate">
                            <Setter.Value>
                            <ControlTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="16"/>
                                    </Grid.ColumnDefinitions>
                                    <AdornedElementPlaceholder Grid.Column="0"/>
                                    <Image Source="bullets16.png" Grid.Column="1" ToolTip="{Binding CurrentItem.ErrorContent, Mode=OneWay}">
                                    </Image>
                                </Grid>
                            </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TextBox.Style>
                <TextBox.Text>
                    <Binding Path="Property" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                    </Binding>
                </TextBox.Text>
            </TextBox>
        </Grid>
    </TabItem>
    <TabItem Header="tabItem2" Name="tabItem2" >
        <Grid />
    </TabItem>
</TabControl>

这是什么,我做错了任何想法?

Any ideas on what I am doing wrong?

推荐答案

标签的物品往往会乱用装饰器(虽然我不知道为什么,我经历了它)。

Tab items tend to mess up with adorners (although I don't know why, I experienced it).

我可以重现你的问题。

通过与AdornerDecorator包裹TabItem的内容解决这个问题。

Solve it by wrapping the contents of the TabItem with an AdornerDecorator.

所以,

<TabControl >
    <TabItem Header="tabItem1" Name="tabItem1" GotFocus="tabItem1_GotFocus">

        <AdornerDecorator>

           <Grid>
           ....
           </Grid>

        </AdornerDecorator>

    </TabItem>
    ...
</TabControl>

这篇关于问题与WPF验证(IDataErrorInfo的)和制表聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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