2016-07-28 51 views
0

我有一個TextBox這表明通過ToolTipIDataErrorInfo驗證信息,通過使用這種風格:在提示顯示IDataErrorInfo的錯誤上的鍵盤焦點和鼠標

<Style x:Key="EntityPropertyTextBoxErrorStyle" TargetType="{x:Type TextBox}" 
     BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <!-- this gets rid of all adornment INCLUDING THE DEFAULT RED BORDER --> 
       <AdornedElementPlaceholder /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="ToolTip" 
       Value="{Binding RelativeSource={RelativeSource Self}, 
       Path=(Validation.Errors)[0].ErrorContent}"/> 
      <Setter Property="Background" Value="MistyRose" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

現在我想的ToolTip到鍵盤上顯示焦點,以及。

回答

0

我發現最好的解決辦法是使用的Popup代替ToolTip

<ControlTemplate x:Key="ErrorTemplate"> 
    <StackPanel> 
     <AdornedElementPlaceholder x:Name="ControlWithError" /> 
     <Popup PlacementTarget="{Binding ElementName=ControlWithError}" Placement="Top"> 
      <Popup.IsOpen> 
       <MultiBinding Converter="{StaticResource AtLeastOneTrueConverter}"> 
        <Binding Path="AdornedElement.IsMouseOver" 
          ElementName="ControlWithError" Mode="OneWay" /> 
        <Binding Path="AdornedElement.IsKeyboardFocusWithin" 
          ElementName="ControlWithError" Mode="OneWay" /> 
       </MultiBinding> 
      </Popup.IsOpen> 
      <Border BorderThickness="1"> 
       <TextBlock Text="{Binding 
            AdornedElement.(Validation.Errors)[0].ErrorContent, 
            ElementName=ControlWithError}" 
          Background="White" 
          FontSize="8" /> 
      </Border> 
     </Popup> 
    </StackPanel> 
</ControlTemplate> 
<Style x:Key="EntityPropertyTextBoxErrorStyle" TargetType="{x:Type TextBox}" 
     BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}" /> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="Background" Value="MistyRose" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
相關問題