WPF调整大小,* vs Auto

时间:2011-09-27 15:52:13

标签: c# wpf

我有一个网格中有2列的XAML,我有一个按钮,当我点击它时,在后面的代码中,我将可见性设置为崩溃,并希望调整屏幕的另一半以尝试采取整个屏幕。折叠部分工作,然后RHS切换到LHS,但它不占用整个屏幕。我尝试使用Auto和Star在HidePlots中调整大小,但它从不占用全屏。我想如果我折叠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会占用屏幕的大部分而不会回到原来的位置。

2 个答案:

答案 0 :(得分:2)

您当前的列定义表示使列B等于列A大小的1.5倍,因此即使隐藏了ColumnB的内容,该列仍将占用屏幕的3/5。 / p>

更改它以便折叠的列具有Width="Auto",并将其内容的Width设置为等于展开时的大小。如果你想保持1.5*默认宽度,我建议使用类似MathConverter之类的东西,根据父网格的宽度确定它的大小。我有一个发布here

的代码
<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>

答案 1 :(得分:1)

您需要将第0列设置为您想要的任何内容(自动,150等)并将第1列设置为*。

您的Grid似乎也在Grid范围内,因此也必须考虑父母的行为。