在后面的代码中查找类型的默认样式 [英] Finding the default style for a type in code behind

查看:81
本文介绍了在后面的代码中查找类型的默认样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中,您可以创建Style作为XAML中控件类型的默认值:

In WPF, you can create a Style that acts as the default for a control type in XAML:

<Style TargetType="{x:Type local:MyControl}">
    . . .
</Style>

然后,当WPF显示该控件时,它将根据其类型从资源中查找Style.

Then, when WPF goes to display that control, it looks up that Style from the resources based on the its type.

我想在我的程序的代码后面做同样的事情.如何找到Style?

I want to do the equivalent of this in the code-behind of my program. How do I find that Style?

推荐答案

您可以使用控件类型作为键在应用程序级资源中搜索样式:

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;

这篇关于在后面的代码中查找类型的默认样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆