将TextBlock拉伸到所有可用宽度

时间:2017-04-23 17:23:31

标签: c# xaml uwp uwp-xaml

我正在创建UWP应用程序,其中一个屏幕具有如下布局:

<ListView>
    <ListViewItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Ellipse
                Width="32"
                Height="32"
                Margin="6"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Fill="LightGray" />
            <TextBlock
                Grid.Column="1"
                Margin="12,6,0,0"
                FontSize="20"
                Text="Here is Long Name" />
            <StackPanel
                Grid.Column="2"
                Orientation="Horizontal"
                HorizontalAlignment="Right">
                <Button Width="25" Height="45" BorderThickness="0">-</Button>
                <TextBlock
                    Width="25"
                    Height="45"
                    Padding="0,5,0,0"
                    FontSize="24"
                    Text="0"
                    TextAlignment="Center" />
                <Button Width="30" Height="45" BorderThickness="0">+</Button>
            </StackPanel>
        </Grid>
    </ListViewItem>
    <ListViewItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Ellipse
                Width="32"
                Height="32"
                Margin="6"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Fill="LightGray" />
            <TextBlock
                Grid.Column="1"
                Margin="12,6,0,0"
                FontSize="20"
                Text="Short name" />
            <StackPanel
                Grid.Column="2"
                Orientation="Horizontal"
                HorizontalAlignment="Right">
                <Button Width="25" Height="45" BorderThickness="0">-</Button>
                <TextBlock
                    Width="25"
                    Height="45"
                    Padding="0,5,0,0"
                    FontSize="24"
                    Text="0"
                    TextAlignment="Center" />
                <Button Width="30" Height="45" BorderThickness="0">+</Button>
            </StackPanel>
        </Grid>
    </ListViewItem>
</ListView>

看起来像这样(我不允许插入图片):https://i.stack.imgur.com/Afvn6.png

我希望最后一栏中的StackPanel位于右侧,无论名称有多长。我假设如果TextBlock延伸到所有可用宽度,情况就会如此,但事实并非如此。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

将此添加到ListView

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>