【发布时间】:2023-04-05 00:31:01
【问题描述】:
我想知道如何在 silverlight 的代码隐藏中从样式对象中检索特定的设置器。
我有一个样式 S,它被正确加载并应用于某个对象。然后在运行时我想改变这种风格的一个特定设置器说它的背景设置器(当然我想要的是改变一个特定设置器的值)。
为此,我查看了 SetterBaseCollection 类型的 Setter collection,并包含所有这种风格的 setter。到目前为止,一切都很好。这个集合似乎包括了所有在 XAML 中定义的 setter,但是我该如何访问它们呢?
当我遍历这个集合时,我正在处理“Setter”类型的对象。但我不知道如何选择包含“背景”属性的那个。
// this works totally fine; the variable myStyle is getting the correct Style
myStyle = this.Resources["myStyle"] as Style;
// the myStyle.Setters collection seems to contain all setters of the style
foreach (Setter s in myStyle.Setters) {
// so now what to do to get the setter that sets the Background property of my style ??
// my naive approach did not work:
if (s.Property.ToString().equals("Background")) {
// do something
}
}
(Setter 对象确实有一个 Name 属性,这实际上正是我想要完成与字符串“Background”的比较 - 并且调试提供该属性真正包含字符串“Background”。...但是这是一个非公共属性!
如果有人有任何建议,那将是很棒 :)
附言我的当地时间是凌晨 03:00,所以我有点累和过度劳累 - 所以请让我再次澄清我的问题,以防你猜不出我需要什么;)
【问题讨论】:
-
H.B 在下面的帖子中是绝对正确的。
Style.IsSealed属性为 true,您不能在运行时更改它。 Set Style Setter value from code-behind at runtime。 Change a window style at runtime worked on VS2008, doesn't work on VS2010
标签: wpf silverlight styles code-behind setter