[英]Get the Style of a Control {StaticResource {x:Type TextBlock}} in code behind
I want to grab the default Style
for a TextBlock
in code behind without ever having adding a custom default TextBlock
Style
to resources in XAML
. 我想在代码后面获取
TextBlock
的默认Style
,而不必在XAML
向资源添加自定义默认TextBlock
Style
。
I've got a method like this: 我有一个像这样的方法:
public TextBlock DrawTextBlockAtPoint(string text, Style style)
{
//...
}
that I want to provide an override that just uses the regular TextBlock
Style
: 我想提供一个只使用常规
TextBlock
Style
的覆盖:
public TextBlock DrawTextBlockAtPoint(string text)
{
var style = GetDefaultStyleForProperty(TextBlock.StyleProperty);
DrawTextBlockAtPoint(text, style)
}
Is there anyway to do this? 反正有没有这样做?
The StaticResource Markup Extension essentially tries to find a resource for the defines key. StaticResource标记扩展本质上试图找到定义键的资源。 If the default style for the TextBlock type can be retrieved using:
{StaticResource {x:Type TextBlock}}
you should be able to get it in code using: 如果可以使用以下命令检索TextBlock类型的默认样式:
{StaticResource {x:Type TextBlock}}
您应该能够使用以下代码获取它:
var defaultTextBlockStyle = FindResource(typeof(TextBlock));
Of course, this needs to be called in a context in which the FindResource methods is defined. 当然,这需要在定义FindResource方法的上下文中调用。 I used it inside my main Window class and it works.
我在我的主Window类中使用它并且它可以工作。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.