有没有办法根据内容调整边框的大小?(Winnrt-Xaml)



我在文本块周围有一个边框,可以创建一个圆角的漂亮背景。但无论我做什么,边框宽度总是其父对象的大小。我想限制它的内容大小。我尝试将宽度绑定到其内容的实际宽度,但对于任何绑定模式都不起作用。

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

一个简单的解决方法是忘记xaml中的Border使用TextBox而不是像这样的TextBlock

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

更新:我再次检查,似乎您忘记设置Border的水平校准

这也起作用:

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

最新更新