views:

62

answers:

2

I have a button, the content template of which contains two TextBlocks. How can I program the button to adjust to the amount of text in the TextBlocks?

I only know what text is going into the buttons at run time, not design time.

I was trying to go down the road of putting the TextBlocks in a Viewbox, but a ViewBox can only have one child element.

Thanks, Mark

+1  A: 

Put the 2 TextBlock's inside a Grid or a StackPanel (depending on how you want them oriented), and don't set any Width Attributes. That way, Width will default to Auto. Set a MinWidth if you would like the Button to be visible when the Text attributes are empty.

Jim McCurdy
A: 

The ViewBox is optional, depending on if you want the button to be fixed-width and fit all the content (then, you want it) or if you want to button width to grow (then leave it out)

<Button HorizontalAlignment="Center">
    <Viewbox Width="300">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Some Text" />
            <TextBlock Text="Some More Text" />
        </StackPanel>
    </Viewbox>
</Button>
Brian Genisio