c#
2010-07-12 85 views 0 likes 
0

我想添加字符串与风格加入到资源字典,这怎么可能如何与样式添加字符串添加到资源字典在WPF

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

我想这个字符串添加到ResourceDictionary中

如何?

ResourceDictionary rd = new ResourceDictionary(); 
rd.MergedDictionaries.Clear(); 
rd.Add("MyStyle", MyStyle); 
Application.Current.Resources.MergedDictionaries.Add(rd); 

不工作....

回答

0

仅仅因为在XAML最初描述的东西并不意味着它在运行时(就像你不能把一个字符串包含了一个C#片段和只是一个字符串运行)。

您必须使用对象表示:

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); 

如果你有一个完整有效的XAML文件,你可以使用XamlReader

相关问题