0

我有下面的代码非常简单,带有 wp7 中的样式资源,

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

我想更改 setter 的源值,例如。“display/tool.png”当我想在我的应用程序代码中运行时,请记住我需要风格的图像蜂鸣:)

我正在运行这样的东西,

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

或类似的东西,

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

我得到NullReferenceException了,应用程序中断了......

4

1 回答 1

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;
于 2013-03-01T10:52:11.103 回答