我要抢默认Style
对于 TextBlock
在后面的代码中没有添加自定义默认值 TextBlock
Style
到 XAML
中的资源.
我有一个这样的方法:
public TextBlock DrawTextBlockAtPoint(string text, Style style)
{
//...
}
我想提供一个只使用常规
TextBlock
的覆盖Style
:public TextBlock DrawTextBlockAtPoint(string text)
{
var style = GetDefaultStyleForProperty(TextBlock.StyleProperty);
DrawTextBlockAtPoint(text, style)
}
有没有办法做到这一点?
最佳答案
StaticResource标记扩展本质上是尝试为定义键查找资源。如果可以使用以下方法检索 TextBlock 类型的默认样式:{StaticResource {x:Type TextBlock}}
您应该能够使用以下代码在代码中获取它:
var defaultTextBlockStyle = FindResource(typeof(TextBlock));
当然,这需要在定义了 FindResource 方法的上下文中调用。我在我的主 Window 类中使用它并且它工作正常。
希望这可以帮助。
关于wpf - 获取控件的样式 {StaticResource {x :Type TextBlock}} in code behind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1406982/