2013-04-12 50 views
3

我在我的ViewModel中實現來對我的一些綁定屬性執行驗證。然後我嘗試使用以下方法來設置並自動顯示它:WPF ToolTip Trigger Not Working

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="True"> 
       <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/> 
       <Setter Property="ToolTip.IsOpen" Value="True"></Setter> 
       <Setter Property="ToolTip.StaysOpen" Value="True"></Setter> 
       <Setter Property="ToolTip.Placement" Value="Bottom"></Setter> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

的問題是,雖然正在建立它不會出現,除非我把我的光標移到輸入。我想在驗證失敗時自動顯示工具提示。

我可以想出很多方法來驗證數據,但我正在尋找一種精簡,易重複使用的解決方案,並且不會混淆我的其他代碼(和UI)。如果有更好的方法要做到這一點,然後我喜歡聽到它。

乾杯

+1

爲什麼不使用'Validation.ErrorTemplate'它會轉到Adorned層並且不會影響總體佈局。如果你的遊標強制顯示一個文本框錯誤的工具提示,那麼它實際上位於一個按鈕或UI中的其他位置,所以ToolTip不是很友好。 – Viv

回答

1

我同意Viv。我使用了一種風格來爲樣式中的文本框定義一個validation.errortemplate。它在控制下添加了一個紅框,並顯示錯誤消息。你可以嘗試這個並調整它。我不認爲強制提示是正確的。如果你真的必須擁有這個功能,我會使用validation.errortemplate中的一個彈出窗口。

<Style x:Key="FTC_ValidateTextBox" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> 
    <Setter Property="FontFamily" Value="Open Sans Condensed"/> 
    <Setter Property="FontSize" Value="19" /> 
    <Setter Property="Margin" Value="3,3,15,6"/> 
    <Setter Property="Padding" Value="10,3"/> 
    <Setter Property="TextWrapping" Value="Wrap" /> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
    <Setter Property="Background" Value="{StaticResource DetailTextBox}" /> 
    <Setter Property="BorderBrush" Value="{StaticResource MediumGray}" /> 
    <Setter Property="BorderThickness" Value="1" /> 
    <Setter Property="Foreground" Value="Black" /> 
    <Setter Property="AllowDrop" Value="true"/> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <Border BorderBrush="{StaticResource MediumRed}" > 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 
         <AdornedElementPlaceholder x:Name="parentTextBox" /> 
         <TextBlock Grid.Row="1" Style="{StaticResource DetailError}" 
          Text="{Binding AdornedElement.(Validation.Errors).CurrentItem.ErrorContent, ElementName=parentTextBox}"/> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="ToolTip" Value="{Binding (Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/> 
      <Setter Property="BorderBrush" Value="{StaticResource MediumRed}"/> 
      <Setter Property="Foreground" Value="{StaticResource MediumRed}"/> 
      <Setter Property="Margin" Value="3,3,15,31"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

我正在使用裝飾的PopUp,但PopUps的問題在於它們始終位於頂部。即使你切換應用程序 - 不太喜歡這種行爲。我會看看你的建議,我認爲強制工具提示是一個好主意的原因是因爲對其餘代碼的影響最小(混亂)。 – rupertmulrenan

+0

如何包裝裝飾的文本框,以便文本不會離開窗口(因爲裝飾器位於單獨的圖層上)? – rupertmulrenan

+0

文本已經按照樣式中的定義進行了包裝。你只需要Ned在樣式設置器屬性中爲元素設置最大寬度 –