2015-04-29 86 views
0

很簡單的問題(我想!),但我似乎無法找到一個簡單的答案。WPF findancestor不工作在第二個tabitem

我已經構建了一個包含tabcontrol和兩個選項卡項目的測試WPF應用程序。在每個tabitem上是一個按鈕,它的內容被綁定到存儲在本地資源字典中的Path中。路徑的填充屬性使用FindAncestor綁定按鈕的Foreground屬性。

問題:在tab1上內容正確顯示,但在tab2上它根本不顯示。如果我刪除FindAncestor綁定並用畫筆替換(例如,白色),則兩個按鈕都可以正確顯示。

我希望我失去了一些簡單的東西,因爲這看起來應該是可能的東西。

代碼:

<Window.Resources> 
    <ResourceDictionary> 
     <Path x:Key="TickIcon2" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Stretch="Uniform" x:Shared="False" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z"/> 
    </ResourceDictionary> 
</Window.Resources> 
<Grid> 
    <TabControl> 
     <TabItem Header="1"> 
      <Button Content="{DynamicResource TickIcon2}" Width="50" Height="50" /> 
     </TabItem> 
     <TabItem Header="2"> 
      <Button Content="{DynamicResource TickIcon2}" Width="50" Height="50" /> 
     </TabItem> 
    </TabControl> 
</Grid> 

回答

0

我發現萬一有人遇到這個問題的解決方案。如果我用「綁定」而不是「DynamicResourse」的路徑正確地顯示在兩個標籤:

<Grid> 
    <TabControl> 
     <TabItem Header="1"> 
      <Button Content="{Binding Mode=OneWay, Source={StaticResource TickIcon2}}" Width="50" Height="50" /> 
     </TabItem> 
     <TabItem Header="2"> 
      <Button Content="{Binding Mode=OneWay, Source={StaticResource TickIcon2}}" Width="50" Height="50" /> 
     </TabItem> 
    </TabControl> 
</Grid> 
0

最有可能擁有資源的資源共享字典(默認)自然的事情。

閱讀:MSDN

您可以通過不共享資源的嘗試(X:共享= FALSE)

<Path x:Key="TickIcon2" x:Shared="False" Fill="{Binding Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Control}}}" Stretch="Uniform" x:Shared="False" Data="F1 M 23.7501,33.25L 34.8334,44.3333L 52.2499,22.1668L 56.9999,26.9168L 34.8334,53.8333L 19.0001,38L 23.7501,33.25 Z"/> 
+0

它已經不共享(見倒數第二個屬性) – OZ10