【问题标题】:change style-setter-property更改样式设置器属性
【发布时间】:2013-03-01 10:37:11
【问题描述】:

我下面的代码很简单,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 并且应用程序崩溃了...

【问题讨论】:

  • 这种风格在哪里?

标签: c# windows-phone-7 xaml styles setter


【解决方案1】:

这真的取决于您的Style 所在的位置,您需要使用x:Key 来查找样式而不是x:Name

如果样式在您的应用程序资源 (App.xaml) 中,这应该可以工作

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

如果它在您的Window/UserControl 的上下文中,您将使用FindResource

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

【讨论】:

  • 谢谢,但我的风格在 旁边,并且 var style = App.Current.Resources["ImageFind1"] 作为 Style;我收到这条消息,对方不认识 FindResource。
  • 那就试试Resources["ImageFind1"];
  • 我想我们已经很接近了,现在我得到了 0x8000ffff 异常!打破 style.Setters.SetValue(Image.SourceProperty, "display/tool.png");
  • 你投的风格是Resources["ImageFind1"] as Style;
  • 是但没有,现在我在你帮我找到资源后找到了解决方案,它不起作用命令 style.Setters.SetValue(Image.SourceProperty, "display/tool.png") ;现在我做了一个设置器,每次我想更改图像时,我都会运行 style.Setters.Clear();然后 style.Setters.Add(...setter...);非常感谢您的帮助,教我钥匙和名称的区别以及正确的风格!
猜你喜欢
  • 1970-01-01
  • 2012-04-25
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多