Finding the default style for a type in code behind

微笑、不失礼 提交于 2019-11-30 19:37:34
ZF.

You can search for the style in the Application-level resources by using the control type as the key:

Style defaultStyle = Application.Current.TryFindResource(typeof(MyControl)) as Style;

object globalStyleDefinedByApp;
Style globalStyle = new Style(typeof(TargetType));
if (Application.Current.Resources.TryGetValue(typeof(TargetType), out globalStyleDefinedByApp))
{
    globalStyle = globalStyleDefinedByApp as Style ?? globalStyle;
}

In case somebody lands here looking for a solution for Universal Windows Projects (UWP), no TryFindResource exists so the above is how you have to do it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!