views:

376

answers:

1

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

I've got a method like this:

public TextBlock DrawTextBlockAtPoint(string text, Style textblockStyle)
{
 //...
}

that I want to provide an override that just uses the regular TextBlock style

public TextBlock DrawTextBlockAtPoint(string text)
{
   Style textblockdefaultstyle = *GetDefaultStyleForProperty(TextBlock.StyleProperty);
   DrawTextBlockAtPoint(text, textblockdefaultstyle)
}

Is there anyway to do this?

+2  A: 

The StaticResource Markup Extension essentially tries to find a resource for the defines key. 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:

var defaultTextBlockStyle = FindResource(typeof(TextBlock));

Of course, this needs to be called in a context in which the FindResource methods is defined. I used it inside my main Window class and it works.

Hope this helps.

siz