开发者

Modifying <Style> from code

I need to modify a Style in r开发者_如何转开发esources from code. I already know I can access the Setters like that :

 Style st = (Style)this.Resources["myStyle"];
 set.Setters.Etc...

But I need to change the value of a specific Setter in that style. Is there any way to do it?

Also, how can I retrieve the appropriate setter in the setters list based on the "Property"

thanks


May not be the "cleanest" way, but I found out somthing that actually works :

Style myStyle = (Style)this.Resources["myStyle"];

SetterBase sb = myStyle.Setters.FirstOrDefault(z => (z as Setter).Property == Rectangle.FillProperty);
int iSetterIndex = myStyle.Setters.IndexOf(sb);

myStyle.Setters[iSetterIndex] = new Setter(Rectangle.FillProperty, newScrollThumbBackground);

It seems like I can't replace the value of the setter so instead I just find the old Setter index using property. Then replace the setter at the found index by a new Setter object.


Something like this must do:

SetterBase sb = style.Setters[0];
Setter s = (Setter)sb;
s.TargetName = "abcd";

(at least in WPF)

Edit:
In Silverlight there's no TargetName, but you can still access Property and Value. So, you say something like if (s.Property == Button.IsEnabled) s.Value = false;.

Edit:
Oh, you found a better solution yourself. :-) Didn't know that it's not allowed to change the Value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜