[英]WPF — Expander when collapsed does not resize other grid columns
I am trying to collapse expander when when it collapse the other grid
columns doesnot take up space. 当其他
grid
列不折叠时,我正在尝试折叠扩展器。
I tried this in expander_collapsed
, the grids are taking the whole space but then I am not able access expander only to make it expand. 我在
expander_collapsed
中进行了尝试,网格占用了整个空间,但是我无法仅通过访问expander来使其扩展。
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 code: 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>
I have 3 columns in my main grid
. 我的主
grid
有3列。 One is expander
and other 2 are grid
. 一个是
expander
,另外两个是grid
。
I want the other two columns to take up the space when expander
is collapsed. 我希望其他两列在
expander
折叠时占用空间。
First, I think your GroupBox should be in your Expander. 首先,我认为您的GroupBox应该在您的Expander中。 You need to fix your grid rows, because those are broken--there is no Grid.Row="3".
您需要修复网格行,因为这些行已损坏-没有Grid.Row =“ 3”。 You need to change the Width to Auto on your Grid in Column 0 in your MainGrid.
您需要在MainGrid的第0列的Grid上将Width更改为Auto。 There are probably other things that need attention, but this should help you get a start on figuring out what you need to do:
可能还有其他需要注意的事情,但这应该可以帮助您开始弄清楚您需要做什么:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.