2014-06-23 104 views
4

我創建了自定義錯誤模板來顯示驗證錯誤。這是我的XAML:顯示自定義錯誤模板上的工具提示

<Style TargetType="Control" x:Key="myErrorTemplate"> 
    <Setter Property="Validation.ErrorTemplate"> 
     <Setter.Value> 
      <ControlTemplate> 
       <DockPanel LastChildFill="True"> 
        <TextBlock DockPanel.Dock="Right" 
           Foreground="Red" 
           FontSize="26" 
           FontWeight="Bold" 
           Text=" !" 
           Margin="0,-8,0,0" /> 
        <Border> 
         <AdornedElementPlaceholder Name="myControl" /> 
        </Border> 
       </DockPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
      Path=(Validation.Errors)[0].ErrorContent}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

當發生驗證錯誤時,這將在TextBox附近放置一個感嘆號。當鼠標懸停在TextBox上時,此模板將顯示錯誤工具提示。當我將鼠標懸停在感嘆號(文本塊)上時,我也想顯示工具提示。我如何實現這一目標?

回答

1

試試這個:

 <TextBlock DockPanel.Dock="Right" 
          Foreground="Red" 
          FontSize="26" 
          FontWeight="Bold" 
          Text=" !" 
          Margin="0,-8,0,0" > 
      <TextBlock.Style> 
        <Style TargetType="TextBlock"> 
         <Style.Triggers> 
         <DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource TemplatedParent}}" Value="True"> 
          <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
     Path=(Validation.Errors)[0].ErrorContent}" /> 
         </DataTrigger> 
         </Style.Triggers> 
        </Style> 
      </TextBlock.Style> 
     </TextBlock> 
+0

,它不工作,我得到了綁定錯誤,BindingExpression路徑錯誤:在「對象」「」控制「(名稱=‘’)」找不到「驗證」屬性。 BindingExpression:路徑= Validation.HasError; DataItem ='Control'(Name ='');目標元素是'TextBlock'(Name ='');目標屬性是'NoTarget'(類型'對象') – Angel

+0

把它們放在括號 – Nitin

+0

,現在我得到了另一個錯誤XamlParse異常,它說:{「TwoWay或OneWayToSource綁定不能在只讀屬性'HasError' System.Windows.Controls.Control'。「} – Angel

相關問題