WPF - 使用Stackpanel

时间:2014-12-17 12:12:58

标签: wpf xaml stackpanel

在我的WPF窗口中,我有一个状态栏和一个TextBox。现在我想要实现的是在我的代码中,我有一个折叠并显示状态栏的按钮。当我单击按钮时,状态栏会折叠,因此控件将为collapsed并且不再显示,因此文本框将填充状态栏的空间。再次按下该按钮时,状态栏将为visible并将向上推文本框。

我只尝试了这个,但它没有用。问题是状态栏会隐藏但文本框仍然在同一个地方而不占用空间。有人请帮助我,我将不胜感激。

<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>

3 个答案:

答案 0 :(得分:1)

正如我们之前所说,为了实现TextBox拉伸的功能,您可以“使用DockPanel,就像这样:

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

还有其他方式,即使用Grid.RowsDefinitions,但为了这次对话,我们坚持DockPanel。 快乐的编码。

答案 1 :(得分:0)

我相信你是在追求这个:

您可以绑定到ToggleButton上的IsChecked属性,并使用转换器将IsChecked布尔值转换为Visibility,这是转换器类:

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

这是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>

我建议您阅读converters

答案 2 :(得分:0)

检查一下:

    <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>

您需要一些转换器,如下所示:

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

GridSpanConverter代码:

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

在这里,您必须使用命名空间在Window中添加本地程序集referance。