2014-02-23 5 views
0

Visual Studio 2010에서 만든 WPF 프로젝트를 Visual Studio 2013으로 업그레이드하고 유효성 검사 방식이 변경되는 문제가 있습니다.2010 년에서 2013 년까지의 WPF 업그레이드 유효성 검사 문제

VS 2010에서는 C# 코드의 이벤트 처리기를 가리키는 Validation.Error 태그를 만들 수있었습니다. 그런 다음 작업을 트랩 할 수 있고 추가 된 경우 유효성 검사 중에 작성된 오류 문자열이있는 메시지 상자를 표시 할 수 있습니다.

또한 키 스트로크를 시뮬레이션하고 포커스를 모든 컨트롤에 설정하고 필요한 모든 작업을 수행하기 위해 이벤트 핸들러를 사용했습니다.

그러나 VS 2013에서는 유효성 검사 태그에 ErrorTemplate이 필요하므로 전체 컨트롤이 아닌 단일 컨트롤에서만 예제가 사용되고 이벤트를 사용할 수있는 방법이 제공되지 않습니다. 처리기를 VS 2010에서 수행하는 것처럼 처리 할 수 ​​있습니다. 누군가가 VS 2013에서 사용자 정의 수준에서이 작업을 수행하는 방법에 대한 예제를 제공 할 수 있습니까? 여기

<UserControl x:Class="PDMonitorClient.LabResultsUserControls.uc24HourBatchOnly" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:igWPF="http://schemas.infragistics.com/xaml/wpf" 
      xmlns:sys="clr-namespace:System;assembly=mscorlib" 
      mc:Ignorable="d" 
      d:DesignHeight="320" 
      d:DesignWidth="797" 
      DataContext="{Binding}" 
      Validation.Error="TwentyFourHourBatchOnlyCollectionSample_Error" 
      x:Name="TwentyFourHourBatchOnlyCollectionSample" 
      Unloaded="TwentyFourHourBatchOnlyCollectionSample_Unloaded" 
      IsVisibleChanged="TwentyFourHourBatchOnlyCollectionSample_IsVisibleChanged" 
      Loaded="TwentyFourHourBatchOnlyCollectionSample_Loaded"> 

가 내 C# 스 니펫입니다 : 다음 XAML 여기

되어 다음

내 질문에 명확성을 추가 VS 2010 년 사용하고 코드 조각의 예입니다

/// <summary> 
     /// This event is fired by WPF binding 
     /// </summary> 
     /// <param name="sender"></param> 
     /// <param name="e"></param> 
     private void TwentyFourHourBatchOnlyCollectionSample_Error(object sender, ValidationErrorEventArgs e) 
     { 
      try 
      { 
       if(this.Visibility == Visibility.Visible) 
       { 
        if(e.Action == ValidationErrorEventAction.Added) 
        { 
         Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() => 
         { 
          MessageBox.Show(e.Error.ErrorContent.ToString(), "Validation Error", MessageBoxButton.OK, MessageBoxImage.Hand); 

          // 
          // The following code will insure that the focus is at the control with the error whether the user taps enter and causes 
          // the message box to popup up, or enters in a value and then clicks on another control. End result, no matter what, 
          // the user is back in the control with the invalid value. 
          // 
          InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.SHIFT, VirtualKeyCode.TAB); 
          InputSimulator.SimulateKeyPress(VirtualKeyCode.TAB); 
          string OriginalSourceTypeName = e.OriginalSource.GetType().Name; 
          if(OriginalSourceTypeName == "XamMaskedEditor") 
          { 
           ((XamMaskedEditor)(e.OriginalSource)).Focus(); 
          } 
         })); 

         e.Handled = true; 
        } 
       } 
      } 
      catch(Exception) 
      { 
       throw; 
      } 
     } 

도움 주셔서 감사합니다.

+0

당신이 생각할 수도 있습니다. 닷넷 4.0 대 4.5 변경, VS이 아니야 – Shoe

+0

질문에 더 세밀한 부분을 넣어 주셔서 감사합니다. 당신이 찾고있는 결과를 얻을 수 있도록 Validation.Error 및 Validation.ErrorTemplate에 대한 생각이 있습니까? – MikeMalter

답변

0

좋아, 대답을 찾았습니다. Intellisense가 Validation.ErrorTemplate에 우선권을 부여한 것처럼 보였습니다. 문제를 강제하고 Validation.Error를 입력 한 다음 템플릿 부분을 삭제하면 필요한 부분을 얻었습니다. 오류가 발생하면 해고 될 이벤트였습니다.

다음 Microsoft 문서를 참조하십시오. http://msdn.microsoft.com/en-us/library/system.windows.controls.validation.error(v=vs.110).aspx 이 이벤트는 나에게 매우 도움이되며이 이벤트에 대한 액세스가 끊어지지 않는다는 사실에 대해 정말 기쁘게 생각합니다.

관련 문제