文本框不显示verticalscrollbar

时间:2014-07-27 12:43:50

标签: wpf textbox grid scrollbar

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="550" MinHeight="350">
    <DockPanel Background="BlanchedAlmond">
        <DataGrid Background="YellowGreen" DockPanel.Dock="Top" MinHeight="100">

        </DataGrid>
        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" FlowDirection="RightToLeft" Margin="10" Background="YellowGreen">
            <Button HorizontalAlignment="Right" Click="Button1Add_Click" Margin="5,0">Add text 1</Button>
            <Button HorizontalAlignment="Right" Click="Button2Add_Click">Add text 2</Button>
            <Button HorizontalAlignment="Right" Click="Button1_Click" Margin="5,0">Toggle textbox 1</Button>
            <Button HorizontalAlignment="Right" Click="Button2_Click">Toggle textbox 2</Button>
        </StackPanel>
        <Grid Background="Red">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <TextBox x:Name="TextBox1" Grid.Row="0" Background="AliceBlue" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">Test 1</TextBox>
            <TextBox x:Name="TextBox2" Grid.Row="1" Background="AliceBlue" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">Test 2</TextBox>
        </Grid>
    </DockPanel>
</Window>

在上面的示例中,我试图实现中间的两个文本框只是用按钮填充顶部数据网格和底部堆栈面板之间的可用空间。

他们必须根据文字内容和可见度划分它们之间的空间 两个属性都可以通过数据绑定进行更改,我使用点击事件进行了模拟。

但是当空间被填满时,他们必须在需要时显示垂直滚动条。

上面的代码示例并不好。当我向文本框添加文本时,网格行变大但从可见区域消失,没有滚动条。

编辑:我最终得到了:

<ScrollViewer Background="AliceBlue" VerticalScrollBarVisibility="Auto">
    <StackPanel Orientation="Vertical">
        <Label x:Name="Label1" Margin="0,6,0,0">Test 1:</Label>
        <TextBox x:Name="TextBox1" Padding="0,6"></TextBox>
        <Label x:Name="Label2" Margin="0,6,0,0">Test 2:</Label>
        <TextBox x:Name="TextBox2" Padding="0,6" ></TextBox>
    </StackPanel>
</ScrollViewer>

1 个答案:

答案 0 :(得分:1)

您需要在XAML中围绕内容添加ScrollViewer。

ScrollViewer on MSDN

<ScrollViewer>
    Content
</ScrollViewer>