Zer0
Zer0

Reputation: 1022

WPF - Using Stackpanel

In my WPF Window, I've got a Status Bar and a TextBox. Now what I want to achieve is in my code, I have a button which collapses and shows the Status Bar. When I click on the button, the Status Bar collapses therefore the control will be collapsed and no longer shown and so the Textbox will fill the space of the Status Bar. When the button is pressed again, the Status bar will be visible and will push the Textbox up.

I've tried only this but it didn't work. THe problem is that the Status Bar would hide but the textbox would still be in the same place and not take up the space. Someone please help me that would be greatly appreciated.

<StackPanel>
<Grid>
    <StatusBar Height="30" VerticalAlignment="Bottom">
        <StatusBarItem Content="Last Saved Not Saved"/>
        <StatusBarItem HorizontalAlignment="Right">
            <StackPanel Orientation="Horizontal">
                <StatusBarItem  Content="Character 0 Word 0"/>
                <StatusBarItem Content="Ln 1, Ch 0"/>
            </StackPanel>
        </StatusBarItem>
    </StatusBar>
    <TextBox x:Name="textBox" Height="380" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Margin="0,24,0,0"/>
</Grid>
</StackPanel>

Upvotes: 0

Views: 522

Answers (3)

XAMlMAX
XAMlMAX

Reputation: 2363

As we spoke before, to achieve functionality of the TextBox stretching, you "could" use DockPanel, like so:

<DockPanel MinHeight="380" LastChildFill="True">
    <StatusBar DockPanel.Dock="Top" Height="30"/>
    <TextBox DockPanel.Dock="Bottom"/>
</DockPanel>

There are also other ways i.e. using Grid.RowsDefinitions but for the sake of this conversation let's stick to the DockPanel.
Happy coding.

Upvotes: 1

Amol Bavannavar
Amol Bavannavar

Reputation: 2072

Check this :

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition x:Name="rowHeight" Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ToggleButton Name="toggle" Width="100" Height="24" Content="Toggle It"/>
        <TextBox x:Name="textBox" TextWrapping="Wrap" Text="TextBox" Grid.Row="1" Grid.RowSpan="{Binding ElementName=toggle, Path=IsChecked, Converter={StaticResource GridSpanConverter}}"/>
        <StatusBar Height="30" VerticalAlignment="Bottom" Grid.Row="2"
               Visibility="{Binding ElementName=toggle, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
            <StatusBarItem Content="Last Saved Not Saved"/>
            <StatusBarItem HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal">
                    <StatusBarItem  Content="Character 0 Word 0"/>
                    <StatusBarItem Content="Ln 1, Ch 0"/>
                </StackPanel>
            </StatusBarItem>
        </StatusBar>
    </Grid>

You need some converter's given as below :

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <local:GridSpanConverter x:Key="GridSpanConverter"/>

GridSpanConverter Code :

public class GridSpanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool && (bool)value == true)
            return 1;
        return 2;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

Here you have to add local assembly referance in your Window with your namespace.

Upvotes: 0

Mike Eason
Mike Eason

Reputation: 9723

I believe you are after this:

You can bind to the IsChecked property on a ToggleButton, and by using a converter, convert the IsChecked boolean to a Visibility, here is the converter class:

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool val = (bool)value;

        return val ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

And here is the XAML:

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:YourNamespace="clr-namespace:YourNamespace"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <YourNamespace:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
<Grid>
    <StackPanel>
        <ToggleButton Name="toggle" Width="100" Height="24" Content="Toggle It"/>

        <StatusBar Height="30" VerticalAlignment="Bottom"
                   Visibility="{Binding ElementName=toggle, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter}}">
            <StatusBarItem Content="Last Saved Not Saved"/>
            <StatusBarItem HorizontalAlignment="Right">
                <StackPanel Orientation="Horizontal">
                    <StatusBarItem  Content="Character 0 Word 0"/>
                    <StatusBarItem Content="Ln 1, Ch 0"/>
                </StackPanel>
            </StatusBarItem>
        </StatusBar>
        <TextBox x:Name="textBox" Height="380" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Margin="0,24,0,0"/>
    </StackPanel>
</Grid>

I would suggest reading up on converters.

Upvotes: 0

Related Questions