2017-02-04 181 views
0

我需要從畫筆動態獲取顏色。
所以,我有一個刷:WPF從畫筆資源獲取畫筆顏色

<SolidColorBrush x:Key="ExampleBrush" Color="Black" /> 

因此,有可能獲得色彩(另刷爲例):

<SolidColorBrush x:Key="AnotherBrush" Color="{Binding Source={StaticResource ExampleBrush}, Path=Color}" /> 

所以,我的問題是怎麼可能得到這個獲取動態資源?
只是因爲如果我改變與ExampleBrush的資源文件 - 的AnotherBrush顏色不改變...

請隨意問任何問題。謝謝

回答

0

您無法使用DynamicResource將AnotherBrush的Color屬性設置爲ExampleBrush的Color。解決方法是使用DynamicResource直接和旁路AnotherBrush引用ExampleBrush:

<TextBlock Text="..." Foreground="{DynamicResource AnotherBrush}" /> 

你並不真的需要AnotherBrush爲TextBlockForeground還是會換每當AnotherBrush變化。結果將是相同的。

你可以「綁定」到AnotherBrush很好,但那麼你將需要明確更新此刷每當ExampleBrush更新:

<TextBlock Text="..." Foreground="{DynamicResource AnotherBrush}" /> 
+0

是的,我嘗試過這種方式,但它不工作。 – AGS17

+0

什麼不行?如果你的代碼不起作用,那麼你需要發佈一個你想要做什麼的例子,如果你希望有人能夠幫助你...當然它可以使用DynamicResource。 – mm8

+0

你確定嗎? https://www.screencast.com/t/33M28GoAvlf(ExampleBrush只是檢查資源是否已更改) 如果我錯了,請糾正我的錯誤 – AGS17