Tuesday, March 15, 2011

Using ContentStringFormat property with TextBlock in ControlTemplate

I have a simple ControlTemplate for buttons which creates a link-like look and feel for them:

 <ControlTemplate x:Key="LinkTemplate" TargetType="{x:Type Button}">
    <TextBlock Text="{TemplateBinding Content}">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="Blue" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="TextDecorations" Value="Underline" />
                        <Setter Property="Cursor" Value="Hand" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="Gray" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</ControlTemplate>

This works fine, but the problem arises when I want to use ContentStringFormat property to format buttons' contents. Since TextBlock doesn't have ContentStringFormat property I can't use template binding.

I also tried using StringFormat when binding button's content, but it seems to be ignored and original content text is passed to TextBlock in template binding.

I guess I could use value converted and pass ContentStringFormat to it as parameter, but it doesn't feel right.

Is there a way to do this using purely XAML or should I just use value converter?

From stackoverflow
  • I did not find docs for "ContentStringFormat" so I may be missing something trivial here.

    How about using Fil Forte's BindableRun within the ControlTemplate so that the Run could be formatted. Lubo Blagoev has created a control which does just that.

  • I haven't any idea of what is ContentStringFormat's usage, but you can use ContentPresenter instead of TextBlock to use this property.

0 comments:

Post a Comment