[英]How can I get a StackPanel to be only as wide as its content?
The following template shows dynamic information in a colored box. 以下模板在彩色框中显示了动态信息 。
However, the box is always the full width of the DockPanel/screen. 但是,此框始终是DockPanel /屏幕的整个宽度 。
I could put a fixed Width in the Border element, but then a very long customer name might get cut off. 我可以在Border元素中放置固定的Width ,但是随后可能会切断一个很长的客户名称。
How can I tell the Border that it should expand and contract its width based on width of its content, something like a <table>
element does in HTML? 如何告诉Border应该根据其内容的宽度扩展和收缩其宽度,就像HTML中的
<table>
元素一样?
HTML: HTML:
<table>
<tr>
<td style="background-color: #eee; padding: 5px">
the table will be as wide as this text
</td>
</tr>
</table>
XAML: XAML:
<Window.Resources>
<DataTemplate x:Key="CustomerTemplateShow">
<Border CornerRadius="5" Background="#eee" Padding="5">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<DockPanel LastChildFill="False" Margin="10">
<ContentControl
DockPanel.Dock="Top"
Content="{Binding SelectedCustomer}"
ContentTemplate="{StaticResource CustomerTemplateShow}"/>
</DockPanel>
最简单的方法是设置边框的HorizontalAlignment =“ Center”(或向左或向右,以布局方式为准)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.