0

以下模板在彩色框中显示动态信息

但是,该框始终是 DockPanel/屏幕的全宽

我可以在 Border 元素中放置一个固定的宽度,但是很长的客户名称可能会被截断。

我如何告诉 Border 它应该根据其内容的宽度来扩展和收缩其宽度,就像<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:

<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>
4

1 回答 1

2

最简单的方法是设置边框的 Horizo​​ntalAlignment="Center"(或左或右,无论您喜欢哪种布局)。

于 2009-06-18T11:59:28.097 回答