【问题标题】:Error Style displayed even if the usercontrol is collapsed/hidden即使用户控件折叠/隐藏也会显示错误样式
【发布时间】:2012-11-29 13:27:32
【问题描述】:

我有一个用户控件来验证它的内容。

我正在使用 IDataErrorInfo 来验证输入(我必须使用 .Net 3.5)。

我正在学习本教程: http://japikse.blogspot.ch/2009/07/idataerrorinfo-error-templates-and-wpf.html

这意味着我正在使用以下样式:

<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="Background" Value="Pink"/>
        <Setter Property="Foreground" Value="Black"/>
    </Trigger>
</Style.Triggers>
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <DockPanel LastChildFill="True" 
               ToolTip="{Binding ElementName=controlWithError,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                <TextBlock DockPanel.Dock="Right" 
                   Foreground="Red"
                   FontSize="14pt" 
                   Margin="-15,0,0,0" FontWeight="Bold">*
                </TextBlock>
                <Border BorderBrush="Red" BorderThickness="1">
                <AdornedElementPlaceholder Name="controlWithError" />
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Setter.Value>
</Setter>

问题是,在某些情况下,我必须隐藏表单(当没有选择任何元素时),但是当我显示的表单没有错误时,然后我折叠表单(网格), UserControls 中的文本框(无效,因为它们不接受空值)有红色边框和星号:

不隐藏时的相同形式: 请注意: 只有ErrorTemplate的内容是可见的,触发器的内容(粉红色背景,黑色前景)不适用。

所以我认为这种风格有问题,但我对 WPF 风格不够熟悉,无法理解原因。

另外一件奇怪的事情: 如果我有具有相同验证的文本框(而不是用户控件中的文本框),它们会被正确隐藏。

编辑 我发现了一些对我有很大帮助的其他东西,首先是这个主题: Hiding validation adornment when hiding a control 有了这个,我做了以下事情:将我的用户控件可见性绑定到隐藏元素可见性,然后在用户控件中,我将文本框可见性绑定到用户控件可见性,然后(最后)添加一个样式触发器:

<Style TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="Background" Value="Pink"/>
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
            <Trigger Property="Visibility" Value="Visible">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <DockPanel LastChildFill="True" ToolTip="{Binding ElementName=controlWithError,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" >
                                <TextBlock DockPanel.Dock="Right" Foreground="Red" FontSize="14pt" Margin="-15,0,0,0" FontWeight="Bold">*</TextBlock>
                                <Border BorderBrush="Red" BorderThickness="1">
                                    <AdornedElementPlaceholder Name="controlWithError" />
                                </Border>
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
</Style>

几乎可以用了!,唯一剩下的就是一个小红点,不知从何而来

有什么想法吗?

【问题讨论】:

    标签: wpf styles idataerrorinfo validation


    【解决方案1】:

    我找到了一种解决方法,它可能不是最好的方法(如果你有更好的方法,请告诉我!)。

    首先,当我使用我的用户控件时,我将它的可见性绑定到我要隐藏的用户控件:

    <userContols:BrowseFileControl  Visibility="{Binding ElementName=uxFormGrid, Path=Visibility}"/>
    

    其次,在 IDataErrorInfo 方法(public string this[string columnName]) 中,我只在显示当前控件时返回错误。

        public string this[string columnName]
        {
            get
            {
                String result=null;
                if (Visibility == Visibility.Visible)
                {
                    if (columnName == "FilePath")
                    {
                        if (String.IsNullOrEmpty(FilePath))
                        {
                            if (!CanBeEmpty)
                            {
                                result = "Mandatory field";
                            }
                        }
                        else if (!IsValidFilePath(FilePath))
                        {
                            result = "Malformed path";
                        }
                    }
                }
                return result;
            }
        }
    

    我更喜欢处理样式,但我没有找到完全去除任何红色标记的方法。

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 2018-11-28
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 2015-07-22
      • 2016-12-15
      • 1970-01-01
      • 2012-08-17
      相关资源
      最近更新 更多