2013-04-12 48 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在样式设置器属性中为元素设置最大宽度 –