开发者

Dynamically change implicit style

Right now I have some TabItems in my App which are implicitly styled. I want to 开发者_开发百科add a "Night mode" to my app and change my style. How should I go about this?


You can do this with merged dictionaries. Put all your "normal" styles in a dictionary and add it to the app resources by default:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Normal.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Then you can remove the current dictionary and load another one dynamically:

private void ChangeStyles()
{
    App.Current.Resources.MergedDictionaries.Clear();

    StreamResourceInfo resInfo = App.GetResourceStream(new Uri("Styles/NewStyles.xaml", UriKind.Relative));
    XDocument xaml = XDocument.Load(resInfo.Stream);
    ResourceDictionary resource = XamlReader.Load(xaml.ToString()) as ResourceDictionary;

    App.Current.Resources.MergedDictionaries.Add(resource);
}


Alfonso was right in idea... but you have to do it like this in WPF

App.Current.Resources.MergedDictionaries.Clear(); 
Uri uri = new Uri("/Resources/GlassButton5Night.xaml", UriKind.Relative);
var resDict = Application.LoadComponent(uri) as ResourceDictionary;
App.Current.Resources.MergedDictionaries.Add(resDict);

And you have make sure you reset your MergedDictionaries at the right level

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜