2012-07-02 3 views
0

저는이 문제를 파악하기 위해 모든 노력을 기울였습니다. :(WPF Validation.HasError TextBox의 여백 설정

텍스트 상자를 들어

, 그것이 유효성 검사 오류를 가지고있는 동안 나는

이 잘 작동합니다. 텍스트 상자의 오른쪽에 이미지와 Validation.ErrorTemplate 설정이! 그러나 한 가지 내가하고 싶은 크기를 조정하거나 오류가있는 텍스트 상자 여백을 설정하여 텍스트 상자가 폼에있는 공간 내에 맞도록 설정합니다. 현재 이미지가 텍스트 상자 영역 외부로 이동합니다 ..

정말로 원하는 것은 오류가있는 텍스트 상자입니다. 없이 텍스트 상자와 같은 공간을 차지합니다.

여기 내 XAML 스타일입니다 :

0 난 그냥 값에 "0,0,20,0"을 사용하기 전에
<Style TargetType="{x:Type TextBox}"> 
<Style.Resources> 
    <my:TextBoxWidthTransformConverter x:Key="TextBoxWidthTransformConverter"/> 
</Style.Resources> 
<Style.Triggers> 
    <Trigger Property="Validation.HasError" Value="true"> 
    <Setter Property="Foreground" Value="Red"/> 
    <Setter Property="Margin" Value="{Binding Converter={StaticResource TextBoxWidthTransformConverter}, RelativeSource={RelativeSource Self}}"/> 
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/> 
    </Trigger> 
</Style.Triggers> 
<Setter Property="Validation.ErrorTemplate"> 
    <Setter.Value> 
    <ControlTemplate> 
     <StackPanel 
     Margin="{TemplateBinding Margin}" 
     Orientation="Horizontal" 
     RenderOptions.BitmapScalingMode="NearestNeighbor" 
     >    
     <AdornedElementPlaceholder 
      Grid.Column="1" 
      Grid.Row="1" 
      Name="controlWithError" 
      /> 
     <Border Width="2"/> 
     <Image 
      ToolTip="{Binding ElementName=controlWithError, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}" 
      Source="imagepath" 
      /> 
     </StackPanel> 
    </ControlTemplate> 
    </Setter.Value> 
</Setter> 

나는 아무 소용, 내가 일이 뭔가를 얻을 수 있다면 바로 볼 수 TextBoxWidthTransformConverter 변환기를 사용하지만 해요 . 변환기는 작동하지 않으며 여백은 변하지 않습니다. Snoop을 사용하여 속성을 변경하거나 변경 한 것을 볼 수 있지만 아무 일도 발생하지 않는지 확인했습니다.

여백은 Validation.HasError 속성으로 변경할 수없는 속성입니까?

모든 통찰력은 훌륭합니다!

감사합니다.

답변

0

이 문제와 관련하여 도움이 될만한 사람을 도우십시오.

왜 Margin 속성이 Validation.HasError 트리거 중에 변경되지 않는지 아직 확실하지 않지만 더러운 작업을 발견했습니다.

해결 방법은 IValueConverter를 사용하여 여백을 설정하는 일부 이벤트 (TextChanged 및 이벤트 정리를위한 언로드)로 Width 속성 바인딩을 사용합니다. TextBoxes Tag 속성을 사용하여 원래 마진을 유지합니다. 제 경우에는 오른쪽 여백 만 걱정합니다.

당신의 Validation.ErrorTemplate에서
public class TextBoxMarginConverter : IValueConverter 
{ 
    private const double TEXTBOX_MARGIN_RIGHT = 25.0; 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 
     return double.NaN; 
     } 

     TextBox textBox = value as TextBox; 

     if (textBox == null) 
     { 
     return double.NaN; 
     } 

     if (Validation.GetHasError(textBox)) 
     { 
     this.SetTextBoxMargin(TEXTBOX_MARGIN_RIGHT, textBox); 
     } 

     return textBox.Width; 
    } 

    private void SetTextBoxMargin(double marginRight, TextBox textBox) 
    { 
     if (textBox.Tag == null) 
     { 
     textBox.TextChanged += new TextChangedEventHandler(this.TextBoxTextChanged); 

     textBox.Unloaded += new RoutedEventHandler(this.TextBoxUnloaded); 

     double right = textBox.Margin.Right + marginRight; 

     textBox.Tag = textBox.Margin.Right; 

     textBox.Margin = new Thickness(textBox.Margin.Left, textBox.Margin.Top, right, textBox.Margin.Bottom); 
     } 
    } 

    private void TextBoxUnloaded(object sender, RoutedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 

     if (textBox == null) 
     { 
     return; 
     } 

     textBox.TextChanged -= new TextChangedEventHandler(this.TextBoxTextChanged); 

     textBox.Unloaded -= new RoutedEventHandler(this.TextBoxUnloaded); 
    } 

    private void TextBoxTextChanged(object sender, TextChangedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 

     if (textBox == null) 
     { 
     return; 
     } 

     if (Validation.GetHasError(textBox)) 
     { 
     this.SetTextBoxMargin(TEXTBOX_MARGIN_RIGHT, textBox); 

     return; 
     } 

     if (textBox.Tag != null) 
     { 
     double tag; 

     double.TryParse(textBox.Tag.ToString(), out tag); 

     textBox.Tag = null; 

     textBox.Margin = new Thickness(textBox.Margin.Left, textBox.Margin.Top, tag, textBox.Margin.Bottom); 
     } 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

이 Validation.HasError 트리거에 컨버터를 적용

<Style.Triggers> 
    <Trigger Property="Validation.HasError" Value="true"> 
    <Setter Property="Width" Value="{Binding Converter={StaticResource TextBoxMarginConverter}, RelativeSource={RelativeSource Self}}"/> 
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/> 
    </Trigger> 
</Style.Triggers> 
관련 문제