2

我的情况是我的文本框必须进行一些验证,验证后它显示红色边框。问题是当我将面板悬停在未经验证的文本框上时,红色边框在文本框上方的面板上仍然可见,即使面板具有完全不透明度,它也可能是 WPF 文本框错误。

我有下面的代码来产生这个问题:Xaml:

<Window x:Class="RedTextBoxFix.MainWindow"
        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"
        xmlns:local="clr-namespace:RedTextBoxFix"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Margin="5">
        <Canvas Panel.ZIndex="6000" Name="panel2" Margin="5">
            <Expander Background="LightGray" ExpandDirection="Right" 
                      Header="Expand over the textbox.." 
                      VerticalAlignment="Top" 
                      HorizontalAlignment="Left">
                <StackPanel Width="900" Height="600">
                </StackPanel>
            </Expander>
        </Canvas>
        <StackPanel Name="panel1" Visibility="Visible" Margin="5">
            <TextBox Name="DataBoundTextBox" Height="20" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Binding Path="TextValue">
                    <Binding.ValidationRules>
                        <ExceptionValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox>
        </StackPanel>
    </StackPanel>
</Window>

文件代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RedTextBoxFix
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.DataContext = new MyClass("RemoveThisText");



        }
        public class MyClass : INotifyPropertyChanged
        {

            private string mTextValue;

            public MyClass(string defaultText)
            {
                TextValue = defaultText;
            }

            public string TextValue
            {
                get
                {
                    return mTextValue;
                }
                set
                {
                    mTextValue = value;
                    if (string.IsNullOrEmpty(mTextValue))
                    {
                        throw new ApplicationException("Text value cannot be empty");
                    }
                    OnPropertyChanged(new PropertyChangedEventArgs("TextValue"));
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, e);
                }
            }
        }
    }
}

制作步骤:

(1) 在 WPF 项目中复制粘贴代码,然后启动应用程序。

(2)删除整个文本并按Tab键,然后你有文本框的红色边框

(3) 展开扩展器。

现在您在扩展面板上有意外的红色边框。这是必须删除的。但是如何?这就是问题,请帮助?

4

1 回答 1

3

An Adorner is a custom FrameworkElement that is bound to a UIElement. Adorners are rendered in an AdornerLayer, which is a rendering surface that is always on top of the adorned element or a collection of adorned elements.

Among other things, adorners are used to provide visual feedback, error in your case. Window has an AdornerDecorator which is on top of everything, and it contains an AdornerLayer. That is where your adorner which indicates an error is rendered. So you'll need one underneath the Canvas, you can just add one around the TextBox.

<StackPanel Name="panel1" Visibility="Visible" Margin="5">
    <AdornerDecorator>
        <TextBox Name="DataBoundTextBox" Height="20" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Binding Path="TextValue">
                <Binding.ValidationRules>
                    <ExceptionValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox>
    </AdornerDecorator>
</StackPanel>

More details on adorners on Microsoft Docs.

于 2017-12-17T11:55:33.570 回答