WPF调整大小,* vs自动

本文关键字:vs 自动 调整 WPF | 更新日期: 2023-09-27 18:09:43

我有一个在网格中有2列的XAML,我有一个按钮,当我点击它时,在后面的代码中,我将可见性设置为崩溃,并想要调整屏幕的另一半以尝试占用整个屏幕。折叠部分工作,然后RHS转移到LHS,但它不会占用整个屏幕。我尝试在hi部署中同时使用Auto和Star来调整大小,但它从不占用全屏。我想如果我折叠LHS,并将RHS的列设置为*,它将占据整个屏幕。任何想法吗?谢谢。

下面的代码让它更清晰:

<Grid Grid.Row="1" x:Name="ExpandableGrid">            
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="1.5*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0" x:Name="TableGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>               
        <GroupBox Grid.Row="0" Grid.Column="0" x:Name="SampleViewGroupBox" Header="SampleView" HorizontalAlignment="Stretch" FontFamily="Arial" FontSize="12"  Margin="5,0,5,0" >
            <ContentControl Content="{Binding LayoutManager.SampleView}" Height="Auto" Width="Auto"/>
        </GroupBox>
        <Button x:Name="TableButton" HorizontalAlignment="Right" Content="Button" Width="15" Height="15" VerticalAlignment="Top" Margin="0,0,-2,0" Click="MaxButton_Click"  Grid.Column="0" Grid.Row="0"/>
    </Grid>
    <Grid Grid.Column="1" x:Name="BaseViewGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <GroupBox Grid.RowSpan="2" Grid.Column="1" Name="BaseViewGroupBox" Header="PLOTS" Margin="5,0,5,0" >
            <ContentControl Content="{Binding LayoutManager.ConsensusView}" Height="Auto" Width="Auto" />
        </GroupBox>
    </Grid>
</Grid>

private void MaxButton_Click(object sender, RoutedEventArgs e)
{
    UIElement senderElement = (UIElement)sender;
    if (_tableMinimized)
    {
        HideTables(false);
        _tableMinimized = false;
        ((Button)senderElement).Style = (Style)FindResource("DashboardDetailsButton");
    }
    else
    {
        HideTables(true);
        _tableMinimized = true;
        ((Button)senderElement).Style = (Style)FindResource("DashboardDetailsButtonReverse");               
    }
}
private void HideTables(bool hide)
{
    if (hide)
    {
        foreach (UIElement child in TableGrid.Children)
            child.Visibility = Visibility.Collapsed;
        for (int i = 0; i < ExpandableGrid.ColumnDefinitions.Count; i++)
            ExpandableGrid.ColumnDefinitions[i].Width = GridLength.Auto;
        ExpandableGrid.ColumnDefinitions[1].MinWidth = 500;
        for (int i = 0; i < ExpandableGrid.RowDefinitions.Count; i++)
            ExpandableGrid.RowDefinitions[i].Height = GridLength.Auto;
        TableButton.Visibility = Visibility.Visible;
    }
    else
    {
        foreach (UIElement child in TableGrid.Children)
            child.Visibility = Visibility.Visible;
        for (int i = 0; i < ExpandableGrid.ColumnDefinitions.Count; i++)
            ExpandableGrid.ColumnDefinitions[i].Width = new GridLength(1, GridUnitType.Star);
        for (int i = 0; i < ExpandableGrid.RowDefinitions.Count; i++)
            ExpandableGrid.RowDefinitions[i].Height = new GridLength(1, GridUnitType.Star);
    }
}

编辑:我也试图改变一行:

ExpandableGrid.ColumnDefinitions[1].MinWidth = System.Windows.SystemParameters.PrimaryScreenWidth-20;

而不是硬编码的500值,看起来是正确的。但是,如果我尝试再次单击按钮以恢复正常,则RHS会占据屏幕的大部分,而不会返回到原始位置。

WPF调整大小,* vs自动

您当前的列定义说使列B等于1.5乘以列A的大小,所以即使列B的内容被隐藏,列仍将占用屏幕的3/5

改变它,使列崩溃有一个Width="Auto",并设置它的内容的Width等于任何大小,它应该是当它展开。如果你想保持1.5*的默认宽度,我建议使用MathConverter之类的东西来确定它应该基于父网格的宽度的大小。我有一个代码张贴在这里

<Grid x:Name="ParentGrid">   
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid x:Name="RHS" Grid.Column="0" />
    <!-- Collapse this Grid -->
    <Grid x:Name="LHS" Grid.Column="1" 
          Width="{Binding ElementName=ParentGrid, Path=ActualWidth,
          Converter={StaticResource MathConverter},
          ConverterParameter=((@VALUE/5)*3)}" /> 
</Grid>

您需要将第0列设置为您想要的任何值(Auto, 150等),并将第1列设置为*。

看起来你的Grid也在Grid内,所以父母的行为也必须考虑进去。