开发者

Set up a style from a resource in code

开发者 https://www.devze.com 2023-03-28 20:15 出处:网络
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.

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

精彩评论

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