为什么文本框边框红色出现在文本框上方的面板上

Why textbox border red color appears on the panel over textbox

我遇到的情况是我的文本框必须进行一些验证,验证后它显示红色边框。 问题是当我将面板悬停在未经验证的文本框上时,红色边框在文本框上方的面板上仍然可见,即使面板完全不透明, 它可能是一个 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) 展开Expander。

现在扩展面板上出现意外的红色边框。必须删除哪个。 但是怎么办?就是这个问题,有什么帮助吗?

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.

除其他外,装饰器用于提供视觉反馈,在您的情况下是错误的。 Window 有一个 AdornerDecorator 位于所有内容之上,它包含一个 AdornerLayer。那是您的装饰器呈现错误的地方。所以你需要在 Canvas 下方添加一个,你可以在 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>

有关 Microsoft Docs 装饰器的更多详细信息。