WPF网格列对齐:从第一个到右边,从第二个到左边

时间:2017-11-07 15:09:05

标签: c# wpf grid

我的WPF应用程序中有一个网格。该网格有两列和一行。

现在,我正在尝试将第一列内容与左侧的右侧和第二列内容对齐,如下所示:

-----------------------------------------------------
|                ProgressControlSpinner|"Loading..."|
-----------------------------------------------------

相反,我获得了:

-----------------------------------------------------
|ProgressControlSpinner|"Loading..."                |
-----------------------------------------------------

请注意,我希望第二列自动调整为文本宽度。 此网格也应该扩展到包含它的外部网格列。

我的网格如下:

<Grid x:Name="MyGrid" Grid.Column="1" Margin="3" Visibility="Collapsed">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>                    
    <controls:ProgressControlSpinner Grid.Column="0" Width="20" Height="20" HorizontalAlignment="Right"/>
    <TextBlock Margin="3 0 0 0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Loading..." />
</Grid>

注意:此网格包含在另一个外部网格中。

1 个答案:

答案 0 :(得分:1)

我已经解决了。在定义包含内部网格的列的宽度时,我的问题出现在外部网格中。

我有:

<Grid Grid.Column="0" Grid.Row="0" Margin="10 10 10 5" HorizontalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>

    <Grid x:Name="MyGrid" Grid.Column="1" Margin="3" Visibility="Collapsed">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>                    
        <controls:ProgressControlSpinner Grid.Column="0" Width="20" Height="20" HorizontalAlignment="Right"/>
        <TextBlock Margin="3 0 0 0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Loading..." />
    </Grid>

</Grid>

正确的是:

<Grid Grid.Column="0" Grid.Row="0" Margin="10 10 10 5" HorizontalAlignment="Stretch">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="auto"></ColumnDefinition>
    </Grid.ColumnDefinitions>


    <Grid x:Name="MyGrid" Grid.Column="1" Margin="3" Visibility="Collapsed">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>                    
        <controls:ProgressControlSpinner Grid.Column="0" Width="20" Height="20" HorizontalAlignment="Right"/>
        <TextBlock Margin="3 0 0 0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Loading..." />
    </Grid>

</Grid>