【问题标题】:Why textbox border red color appears on the panel over textbox为什么文本框边框红色出现在文本框上方的面板上
【发布时间】:2017-12-16 19:10:47
【问题描述】:

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

现在您在扩展面板上有了意想不到的红色边框。这是必须删除的。 但是如何?就是这个问题,有什么帮助吗?

【问题讨论】:

标签: wpf xaml controltemplate contentpresenter validationrule


【解决方案1】:

装饰器是绑定到 UIElement 的自定义 FrameworkElement。装饰器在 AdornerLayer 中渲染,这是一个始终位于被装饰元素或被装饰元素集合之上的渲染表面。

除其他外,装饰器用于提供视觉反馈,在你的情况下是错误的。 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上了解更多关于装饰器的详细信息。

【讨论】:

  • 这是一个很好的答案,我想说@djomlastic :)
猜你喜欢
  • 2016-05-16
  • 2014-08-05
  • 2014-02-09
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多