开发者

how to use style defined in xaml resource dictionary?

I have a a IMultiValueConverter that dynamically creates TextBlock controls. The issue is that it has no styles.

How can I tell my new Te开发者_运维技巧xtBlock to use a style that was defined in my XAML resource dictionary?


See the following question: how to use DynamicResource in the code behind?

Use SetResourceReference, it's equivalent to use DynamicResource in Xaml

So if your Style has the Key myTextBlockStyle

TextBlock textBlock = new TextBlock();
textBlock.SetResourceReference(FrameworkElement.StyleProperty, "myTextBlockStyle");


I have never tried this before, and depending on what your converter is doing, I think if your XAML resource dictionary is external, then link it into the Window where you are displaying the TextBlocks:

<Window.Resources>
    <ResourceDictionary Source="[the path to the resource dictionary]"/>
</Window.Resources>

Then in your textblocks, ensure they have the Style attached that is defined in the resource dictionary. If the textblocks are being created in code behind I believe you should be able to use FindResource to locate the style that is linked in by the resource dictionary. Then do something like this:

textBlock1.Style = (Style)FindResource("myTextBlockStyle");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜