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>
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.
<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 ...
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With