[英]WPF label horizontal content alignment with flexibel width
我有一个TabControl
和一些TabItems
。
TabItems
的 Header 由一个UserControl
和一个label
和一个button
组成。 为此,我遵循了本教程。
label 的水平label
不起作用,因为宽度设置为自动。 这会导致文本居中,并且buttons
不会一直位于它们应位于的右侧。
但我需要宽度灵活,以免长文本被截断。
这是宽度设置为自动,这是固定宽度。
我希望它看起来像具有固定宽度但具有灵活宽度的图片,具体取决于标签的长度。
编辑:我不使用评论中提出的建议中的主题。
这是我的 XAML 代码:
<UserControl
x:Class="CSM.UserControls.CloseableHeaderUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="81" Margin="0" Height="20.5" >
<Grid Margin="0,4,0,0">
<Button Content="X" Name="bClose" FontFamily="Courier" FontWeight="Bold" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" FontStretch="Normal" FontSize="14" ToolTip="Close" Height="20" Width="20" HorizontalAlignment="Right" Margin="0,-4,0,0"/>
<Label Content="TabItem" Name="lTabTitle" FontFamily="Courier" FontSize="12" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Width="auto" Height="24" VerticalAlignment="Top" Margin="0,-4" />
</Grid>
</UserControl>
将 DockPanel 与 LastChildFill 一起使用:
<DockPanel LastChildFill="True">
<Button DockPanel.Dock="Right" ... />
<Label DockPanel.Dock="Left" ... />
</DockPanel>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.