2013-03-01 38 views
0

我有下面的代碼是非常簡單的在WP7風格的資源,改變風格引領者屬性

<Style x:Name="image_find" x:Key="ImageFind1" TargetType="Image"> 
    <Setter Property="Source" Value="display/pen.png"/> 
</Style> 

我想改變二傳手的源值,爲前。 「顯示/ tool.png」當我想在我的應用程序的代碼的運行時旁邊,採取記住,我需要的圖像風格:)

我正在這樣的事情,

image_find.Setters.SetValue(Image.SourceProperty, "display/tool.png"); 

什麼beeing像,

style = App.Current.Resources["image_find1"] as Style;  
style.Setters.SetValue(Image.SourceProperty, "display/tool.png");` 

和我得到NullReferenceException和應用程式的空檔......

+0

此款式在哪裏? – 2013-03-01 10:48:00

回答

2

這真的取決於您的Style所在,而你需要使用x:Key找到樣式不是x:Name

如果樣式是在你的應用資源(App.xaml中)這應該

var style = App.Current.Resources["ImageFind1"] as Style; 

如果其範圍內的Window/UserControl您將使用FindResource

var style = FindResource("ImageFind1") as Style; 
+0

謝謝,但我的風格是旁邊的和與var樣式= App.Current.Resources [「ImageFind1」]作爲樣式;我鋼獲取此消息另一個不識別FindResource。 – 2013-03-01 11:20:38

+0

然後嘗試'Resources [「ImageFind1」];' – 2013-03-01 11:27:20

+0

我在想我們已經接近了,現在我正在獲得0x8000ffff的豁免!打破style.Setters.SetValue(Image.SourceProperty,「display/tool.png」); – 2013-03-01 11:33:24