In Silverlight, how to I adjust the width of a button which contains two TextBlocks?
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 tim开发者_JAVA技巧e, 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
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.
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>
精彩评论