Set up a style from a resource in code
Suppose that I have a style that's been defined in a X开发者_StackOverflow社区AML file.
I want to compile that XAML file into my class library as a resource.Then, I have a control (a custom telerik gridview column) that needs that style. In code, I would like to load that XAML resource, and instance a Style object from it. How would I do that?
I would use merged dictionaries, something like this should work as long as the style you need is in the xaml file:
using (StreamReader sr = new StreamReader(
Application.GetResourceStream(new Uri("Themes/Theming.Blue.xaml", UriKind.Relative)).Stream))
{
xaml = sr.ReadToEnd();
sr.Close();
}
ResourceDictionary rd = (ResourceDictionary)XamlReader.Load(xaml);
App.Current.Resources.MergedDictionaries.Clear();
App.Current.Resources.MergedDictionaries.Add(rd);
精彩评论