0

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>
6
  • Have you tried using DockPanel instead? That is without Grid inside it. You would have to use DockPanel.Dock to set the Controls in the right places. Another thing use Height property on the DockPanel rather than the TextBox, this way TextBox will stretch. HTH Commented Dec 17, 2014 at 12:27
  • Would I have to get rid of the Grid? and instead replace the GRID and the STACKPANEL with a DOCKPANEL? Commented Dec 17, 2014 at 12:31
  • 1
    It would look like this: <DockPanel MinHeight="380" LastChildFill="True"><StatusBar DockPanel.Dock="Top" Height="30"/><TextBox DockPanel.Dock="Bottom"/>. This is of top of my head so you might want to tweak it a bit, but you should get the idea. Commented Dec 17, 2014 at 12:36
  • Sweet as man! @XAMlMAX Looks like it did the job. Commented Dec 17, 2014 at 12:50
  • 1
    If you want I'll answer the question and show you a way without using any converters? Commented Dec 17, 2014 at 13:05

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

I think OP has got that functionality done, i.e. the hiding and showing, the problem lies within xaml it self, not allowing the TextBox to take the rest of the space.
@Mike No need to create BooleanToVisibilityConverter because microsoft already provided it with .net framework. Refer this link msdn.microsoft.com/en-us/library/…
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.