开发者

WPF resizing, * vs Auto

I have a XAML with 2 columns in a Grid and I have a button that when I click it, in the code behind, I set the visibility to collapse, and want to resize the other开发者_开发问答 half of the screen to try to take up the whole screen. The collapsing part works, and the RHS then shifts over to the LHS, but it does not take up the entire screen. I tried using both the Auto and Star to resize in HidePlots, but it never takes the full screen. I thought if I collapsed the LHS, and set the column to * for the RHS, it would take up the whole screen. Any thoughts? Thanks.

Here's some code to make it more clear:

<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);
    }
}

Edit: I tried to also change one line to:

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

instead of the hard-coded 500 value, it looks correct. However, if I try to click the button again to revert back to normal, the RHS takes up the bulk of the screen without getting back to its original position.


Your current column definition says to make Column B equal to 1.5 times the size of Column A, so even if ColumnB's content is hidden, the column will still take up 3/5 of the screen.

Change it so the column that collapses has a Width="Auto", and set it's Content's Width equal to whatever size it should be when it's expanded. If you want to keep the 1.5* default width, I'd recommend using something like a MathConverter to figure out what size it should be based on the parent Grid's width. I have the code for one posted 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>


You need to set column 0 to be whatever you desire (Auto, 150, etc...) and set column 1 to be *.

It looks like your Grid is also within a Grid, so the parent's behavior also has to be taken into account.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜