【问题标题】:WPF label horizontal content alignment with flexibel widthWPF标签水平内容对齐与flexibel宽度
【发布时间】:2021-03-30 21:09:15
【问题描述】:

我有一个TabControl 和一些TabItems

TabItems 的标头由 UserControllabelbutton 组成。 为此,我关注了tutorial

label 的水平对齐不起作用,因为宽度设置为自动。 这会导致文本居中,buttons 并非一直位于它们应位于的右侧。

但我需要宽度灵活,以免长文本被截断。

这是宽度设置为自动,这是固定宽度。

我希望它看起来像具有固定宽度但根据标签长度具有灵活宽度的图片。

编辑:我不使用 cmets 中提出的建议中的主题。

这是我的 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>

【问题讨论】:

标签: wpf width tabcontrol text-alignment tabitem


【解决方案1】:

将 DockPanel 与 LastChildFill 一起使用:

<DockPanel LastChildFill="True">
  <Button DockPanel.Dock="Right" ... />
  <Label DockPanel.Dock="Left" ... />      
</DockPanel>

【讨论】:

  • 这不太对劲。尽管水平对齐设置为左侧,但文本在右侧对齐:link
  • 需要设置对齐方式:
  • 当我添加这个时,外观也没有任何变化。除非我将宽度从自动设置为固定大小
  • 您不需要为标签设置宽度。让 DockPanel 管理它。
猜你喜欢
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-08
  • 1970-01-01
相关资源
最近更新 更多