简体   繁体   中英

WPF label horizontal content alignment with flexibel width

I have a TabControl with some TabItems .

The Header of the TabItems consists of a UserControl with a label and a button . For this i followed this tutorial .

The horizontal alignment of the label does not work because the width is set to auto. This causes the text to be centred and the buttons are not all the way to the right where they should be.

But I need the width to be flexible so that no long text is cut off.

This is with the width set to auto and this is with a fixed width.

在此处输入图像描述 在此处输入图像描述

I want it to look like the picture with the fixed width but with a flexible width depending on the length of the labels.

Edit: I don´t use a theme like in the suggestion brought up in the comments.

This is my XAML code:

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

Use the DockPanel with LastChildFill:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM