Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 228
Smeegs Avatar asked Feb 08 '13 17:02

Smeegs


People also ask

How do you add a border in XAML?

To apply a border to a XAML element, you can place the element within a Border element, The BorderThickness and BorderBrush are two main properties of a Border The BorderBrush property represents the brush that is used to draw the border. The BorderThickness property represents the thickness of the border.

How do you make a grid border in WPF?

<Grid> <Border BorderBrush="Black" BorderThickness="2"> <Grid Height="166" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479" Background="#FFF2F2F2" /> </Border> ... and so on ...


1 Answers

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>
like image 112
iltzortz Avatar answered Sep 30 '22 12:09

iltzortz