WPF - 折叠时展开器不会调整其他网格列的大小

时间:2016-09-14 18:19:55

标签: c# wpf

我试图折叠扩展器,当它崩溃时,其他grid列不会占用空间。

我在expander_collapsed中尝试了这个,网格占用了整个空间但是我无法访问扩展器只是为了扩展它。

 private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            groupBoxMain.Visibility = Visibility.Visible;
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            MainGrid.ColumnDefinitions[0].Width = new GridLength(1, GridUnitType.Star);
            groupBoxMain.Visibility = Visibility.Hidden;
        }

xaml代码:

<Grid x:Name="MainGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="90*"/>
            <ColumnDefinition Width="177*"/>
            <ColumnDefinition Width="133*"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="Auto" Width="Auto">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="8*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="680*"/>
                <RowDefinition Height="31*"/>
            </Grid.RowDefinitions>
            <GroupBox Name="groupBoxMain"  BorderBrush="Black" Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="0" Margin="0,0,0,6">
                <DataGrid Background="DarkGray" CanUserResizeColumns="True" CanUserSortColumns="True">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding BackupVersionNumber}" Header="File Name" MinWidth="100" FontFamily="Default Font" FontSize="16"/>
                        <DataGridTextColumn Binding="{Binding BackupDateTime}" Header="Total Time" MinWidth="100" FontFamily="Default Font" FontSize="16"/>
                        <DataGridTextColumn Binding="{Binding SavedBy}" Header="Total Packet" MinWidth="100" FontFamily="Default Font" FontSize="16"/>
                    </DataGrid.Columns>
                </DataGrid>
            </GroupBox>
            <Expander ExpandDirection="Right"  HorizontalAlignment="Left" VerticalAlignment="Top" Grid.Column="0"  IsExpanded="True" Width="35" Grid.Row="0" Expanded="Expander_Expanded" Collapsed="Expander_Collapsed">
                <Expander.Header>
                    <TextBlock Text="Select Manifest File" Width="Auto"
                           FontSize="11">
                        <TextBlock.LayoutTransform>
                            <TransformGroup>
                                <RotateTransform Angle="90"/>
                            </TransformGroup>
                        </TextBlock.LayoutTransform>
                    </TextBlock>

                </Expander.Header>
                <Grid Background="#FFE5E5E5" Width="0"/>
            </Expander>
        </Grid>
        <Grid HorizontalAlignment="Stretch" Grid.Column="1" Grid.ColumnSpan="2" VerticalAlignment="Stretch" Height="Auto" Width="Auto" Margin="2,0,0,0.2">
                <Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="6" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Margin="0,10.4,0.267,0.2" Background="{DynamicResource {x:Static SystemColors.AppWorkspaceBrushKey}}" />

        </Grid>
    </Grid>

我的主grid中有3列。一个是expander,另外两个是grid

我希望其他两列在expander折叠时占用空间。

1 个答案:

答案 0 :(得分:2)

首先,我认为您的GroupBox应该在您的Expander中。你需要修复网格行,因为它们已经坏了 - 没有Grid.Row =“3”。您需要在MainGrid的第0列中将网格上的宽度更改为自动。可能还有其他需要注意的事情,但这可以帮助您开始弄清楚您需要做什么:

<Grid x:Name="MainGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="177*" />
        <ColumnDefinition Width="133*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="680*" />
        <RowDefinition Height="31*" />
    </Grid.RowDefinitions>
    <Grid Grid.Column="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="8*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Expander ExpandDirection="Right"
                  Grid.Column="0"
                  IsExpanded="True">
            <Expander.Header>
                <TextBlock Text="Header">
                    <TextBlock.LayoutTransform>
                        <RotateTransform Angle="90" />
                    </TextBlock.LayoutTransform>
                </TextBlock>
            </Expander.Header>
            <GroupBox Name="groupBoxMain"
                      BorderBrush="Black"
                      Grid.Row="0"
                      Margin="0,0,0,6">
                <DataGrid Background="DarkGray"
                          CanUserResizeColumns="True"
                          CanUserSortColumns="True">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding BackupVersionNumber}"
                                            Header="File Name"
                                            MinWidth="100"
                                            FontFamily="Default Font"
                                            FontSize="16" />
                        <DataGridTextColumn Binding="{Binding BackupDateTime}"
                                            Header="Total Time"
                                            MinWidth="100"
                                            FontFamily="Default Font"
                                            FontSize="16" />
                        <DataGridTextColumn Binding="{Binding SavedBy}"
                                            Header="Total Packet"
                                            MinWidth="100"
                                            FontFamily="Default Font"
                                            FontSize="16" />
                    </DataGrid.Columns>
                </DataGrid>
            </GroupBox>
        </Expander>
    </Grid>
    <Grid Grid.Column="1"
          Grid.ColumnSpan="2"
          Height="Auto"
          Width="Auto"
          Margin="2,0,0,0.2">
        <Grid Grid.Row="3"
              Grid.Column="0"
              Grid.ColumnSpan="6"
              Margin="0,10.4,0.267,0.2"
              Background="{DynamicResource {x:Static SystemColors.AppWorkspaceBrushKey}}" />
    </Grid>
</Grid>

enter image description here