开发者

how to add string with the style add into ResourceDictionary in wpf

i want to add string with the style is added into ResourceDictionary, how this is possible

Ex : string MyStyle = "<Style x:Key='baseStyle' TargetType='{x:Type Button}'>" +
    "<Setter Property='FontSize' Value='12' />" +
    "<Setter Property='Background' Value='Orange' /></Style>";

i want to add this string into resourcedictionary

How?

ResourceDictionary rd = new ResourceDictionary();
rd.MergedDictionaries.Clear();
rd.Add("MyStyle", MyStyle);
Application.Current.Resources.MergedDictionar开发者_如何学编程ies.Add(rd);

is not working....


Just because something was originally described in XAML does not mean it's a string at runtime (just like you can't take a string containing a C# fragment and just run it).

You have to use the objects representation:

MyStyle = new Style(){TargetType=typeof(Button)};
MyStyle.Setters.Add(new Setter(){Property="FontSize", Value=12});      
MyStyle.Setters.Add(new Setter(){Property="Backround", Value=Brushes.Orange});
Application.Current.Resources.Add("MyStyle",MyStyle);

If you have a complete valid XAML file you can use XamlReader

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜