简体   繁体   中英

How to dock an element to another while respecting parent container size?

I want to layout two labels in a fixed width column. One left-aligned and growing to the right, the other directly to the right of it.

When the combined width of the labels is smaller than the available space there should be empty space to the right. This is achieved with this markup:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <TextBlock TextTrimming="CharacterEllipsis" Text="Sample Name" />        
    <Label Content="Text" Foreground="#AAAAAA" Grid.Column="1"/>
</Grid>

第一种情况,左标签短

When the first label is too large, the text is trimmed and the label at the right should not be pushed out of the column. This is achieved with this markup:

<DockPanel LastChildFill="True">
    <Label Content="Text" Foreground="#AAAAAA" DockPanel.Dock="Right" />
    <TextBlock TextTrimming="CharacterEllipsis" Text="A Really Really Long Sample Name" />        
</DockPanel>

第二种情况,左侧标签过长

How can I get the same results (the screenshots) without different pieces of Xaml?

Turns out it was something that I thought I had tried. This did it for me:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>
    <TextBlock TextTrimming="CharacterEllipsis" Text="Sample Name" />
    <Label Content="Text" Foreground="#AAAAAA" Grid.Column="1" />
</Grid>

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