获取特定 TargetType 的样式列表
我想获得适用于特定控件类型的样式列表。我想做类似下面的代码的操作,但不必指定键名称并获取适用资源的列表。这可能吗?
ComponentResourceKey key = new ComponentResourceKey(typeof(MyControlType), "");
Style style = (Style)Application.Current.TryFindResource(key);
I want to attain a list of styles that apply to a specific control type. I'd like to do something like the code below, but not have to specify a key name and get a list of applicable resources back. Is this possible?
ComponentResourceKey key = new ComponentResourceKey(typeof(MyControlType), "");
Style style = (Style)Application.Current.TryFindResource(key);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先检查控件的
Resources
,然后继续沿VisualTree
向上检查Resources
来模拟 WPF 的工作方式是否有意义找到您的控件的资源(包括样式
)?例如,也许您可以创建一个扩展方法,在给定
FrameworkElement
的情况下执行此操作:要查看此方法是否有效,请创建在多个级别上同时具有隐式和显式
Styles
的 XAML 文件在VisualTree
中:并在后面的代码中使用以下处理程序:
在此示例中,为
myButton
找到 4 种样式,所有样式都有一个TargetType的
按钮
。我希望这可以成为一个好的起点。干杯!Would it make sense to first examine the
Resources
of your control, and then continue walking up theVisualTree
examiningResources
along the way to simulate how WPF finds resources for your control (includingStyles
)?For example, maybe you could create an extension method that does this given a
FrameworkElement
:To see that this works, create XAML file that has both implicit and explicit
Styles
at multiple levels in theVisualTree
:and with the following handler in the code behind:
In this example, 4 styles are found for the
myButton
all of which have aTargetType
ofButton
. I hope this can be a good starting point. Cheers!