Search code examples
wpfxamltemplates

ControlTemplate TextBlock Style


When I have TextBlock. Run Text = "(" Run Text={Binding IncomeLoss} Run Text=")" in a Page it comes out looking ok, but when I have the same thing inside a ControlTemplate that I apply to a class that derives from Control, there is extra space after each character like "( 100 ) ".

I read that ControlTemplate is a barrier to style inheritance, but how do I guess what exactly style parameter is the one missing that is usually inherited by the Textblock on a Page?


Solution

  • Make sure that you have put all Run elements on the same line in the ControlTemplate.

    There is a difference in output between this:

    <TextBlock><Run Text = "(" /><Run Text="{Binding IncomeLoss}"/><Run Text=")"/></TextBlock>
    

    ...and this:

    <TextBlock>
        <Run Text = "(" />
        <Run Text="{Binding IncomeLoss}"/>
        <Run Text=")"/>
    </TextBlock>