Jul 112012
 
 July 11, 2012  Posted by at 11:10 pm Uncategorized, WinRT Tagged with:  Add comments

Answered this question on Stackoverflow recently. To let a parent element of a textblock have the same size and width as the textblock a simple wrap with width and height set to auto wont do it. But why would you want to do such a thing anyway?
Well you might:
1. Want to follow the recommendation to localize your app, which means that the text can differ in size based on the language- and setting a fixed size might cut the text of,- or allow to much space.
2. Want to be able to dynamically change the text and keep (for example) the border snug
3. or something else

One way to solve this:
[sourcecode language=”XML”]
//

<Border BorderBrush="AliceBlue" Background="SlateBlue" Width="{Binding ElementName=tb, Path=ActualWidth}" Height="{Binding ElementName=tb, Path=ActualHeight}">
<TextBlock FontSize="45" Name="tb" Text="12345" Foreground="AliceBlue"/>
</Border>

//
[/sourcecode]

  One Response to “XAML/WinRT/Metro App Q n’ A: How to set border to TextBlock height and width?”

  1. I have tried something similar (bind FontSize to ActualHeight of TextBox) but in Metro/Windows store apps this does not seem to work.
    AcutalHeight reports 0 at the time when the binding is executed.
    Later on ActualHeight (e.g. in GotFocus handler) reports correct values.

    I have not yet found a XAML-only solution to bind FontSize to Control.Height.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 3 + 12 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)