2016-09-14 54 views
0

我試圖摺疊擴展器時,當它摺疊其他grid列不佔用空間。WPF - 摺疊時的擴展器不調整其他網格列

我在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摺疊。

+0

的根據XAML可能有助於回答這個問題,以及 – DonGru

+0

增加了xaml代碼也是如此 – PSDebugger

回答

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