2017-12-16 23 views
1

일부 유효성 검사를 수행해야하는 텍스트 상자가있는 상황에서 유효성 검사를하면 빨간색 테두리가 표시됩니다. 문제는 유효성이 확인되지 않은 텍스트 상자 위에 패널을 가져 가면 패널에 전체 불투명도가있는 경우에도 빨간색 테두리가 여전히 텍스트 상자 위에있는 패널에 표시됩니다. 아마도 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); 
       } 
      } 
     } 
    } 
} 

생산 단계 :

나는이 문제를 생산하는 아래의 코드를

WPF에서

(1) 복사 붙여 넣기 코드 프로젝트 및 lauch 응용 프로그램.

(2) 전체 텍스트를 눌러 탭을 제거하고 당신은

(3) 확장기를 확장 텍스트 상자의 빨간색 테두리가 있습니다.

이제 확장기 패널에서 예기치 않은 빨간색 테두리가 나타납니다. 어느 것이 제거되어야합니다. 하지만 어떻게? 그게 뭐야, 어떤 도움을 주시겠습니까?

+0

가능한 [wpf 오류 템플릿 - 확장자 축소시 빨간색 상자가 계속 표시됨] (https://stackoverflow.com/questions/1471451/wpf-error-template-red-box-still-visible-on- 붕괴 - 중 - 팽창기) – Clint

답변

1

Adorner는 UIElement에 바인딩 된 사용자 지정 FrameworkElement입니다. adorners는 AdornerLayer에서 렌더링됩니다. AdornerLayer는 항상 장식 된 요소 또는 장식 된 요소의 컬렉션 위에있는 렌더링 표면입니다.

다른 이유 중 하나는 시각적 피드백과 오류를 제공하는 데 사용되는 것입니다. 창에는 AdornerDecorator이 있으며 모든 내용 위에는 AdornerLayer이 있습니다. 그것은 오류를 나타내는 귀하의 adorner가 렌더링되는 곳입니다. 따라서 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에 있습니다.

+0

그레이트 대답은 내가 말할 것이라고 @ djomlastic :) –