Smeegs
Smeegs

Reputation: 9224

Is there a way to size a border to its contents? (Winnrt Xaml)

I have a border around a textblock to create a nice background with rounded corners. But no matter what I do the border width is always the size of its parent. I want to limit it to the size of its contents. I tried binding the width to the actual width of it's contents, but that didn't work, with any of the binding modes.

<Border x:Name="TagPreviewBorder" CornerRadius="5"
        Width="{Binding ElementName=TagPreviewTextBlock, Path=ActualWidth, Mode=TwoWay}">
   <TextBlock x:Name="TagPreviewTextBlock"/>
</Border>

Upvotes: 10

Views: 9327

Answers (1)

iltzortz
iltzortz

Reputation: 2422

An easy workarround would be to forget Border in your xaml and use a TextBox instead of TextBlock like this:

<TextBox Text="Your Text Here" 
         IsReadOnly="True" Background="Transparent" BorderBrush="Red" 
         BorderThickness="3" HorizontalAlignment="Left"/>

UPDATE: I checked again and seems that you have forgotten to set the Border's HorizontalAlignment

This also works:

    <Border CornerRadius="5" HorizontalAlignment="Left" BorderThickness="10">
        <TextBlock Text="My Text Here"></TextBlock>
    </Border>

Upvotes: 18

Related Questions