2013-10-17 37 views
1

我有一個工具提示的樣式和模板,並希望將工具提示中的某些內容綁定到父項的錯誤集合。我可以通過在下面的代碼中顯式設置AncestorType來獲得這個工作,但我希望這可以應用到全局。我嘗試過使用UIElement和FrameworkElement,沒有運氣,但我認爲這是因爲它沒有在樹上找到正確的元素。綁定到未知類型的父項

<ControlTemplate.Triggers> 
      <DataTrigger 
       Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=(Validation.HasError)}" 
       Value="True"> 
       <Setter 
       Property="Visibility" 
       TargetName="ErrorBorder" 
       Value="Visible" /> 
       <Setter 
       Property="Text" 
       TargetName="ErrorText" 
       Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=(Validation.Errors)[0].ErrorContent}" /> 
      </DataTrigger> 
      </ControlTemplate.Triggers> 
+1

所以你希望這個任務能夠讓邏輯樹冒泡,直到找到一個可以綁定的元素,然後賦值它?我不會在Xaml中做到這一點。 – ouflak

回答

0

您可以在RelativeSource中使用AncestorLevel。

+0

這是假設有驗證的父元素總是在同一級別,我不能保證。我已經嘗試過使用Control,FrameworkElement和UIElement這種方法,但沒有運氣。 – MCRXB

1

簡單只是嘗試,

AncestorType=Control 
0

我能夠通過使用綁定到提示的PlacementTarget得到這個工作以下綁定:

{Binding RelativeSource={RelativeSource Mode=Self}, Path=PlacementTarget.(Validation.HasError)}" 

這似乎只要運作良好因爲工具提示的PlacementTarget是具有錯誤驗證的控件,在我的情況下是這樣。