ListBox包含在ScrollViewer中

时间:2011-08-12 08:04:33

标签: c# xaml listbox scrollviewer

我想在屏幕上无法显示所有信息时显示页面的ScrollViewer(即调整窗口大小) 但是,这里的ListBox没有得到滚动,除非我将它设置为具有MaxSize,否则它将获得草图直到页面底部。有没有办法优先让ListBox在我创建ScrollViewer之前显示它?

我现在拥有什么 http://i.imgur.com/bEJcz.png

我想要实现的目标,但我使用MaxHeight作为ListBox here

这是我的标记:

<ScrollViewer HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto"  Name="scrollViewer1"  VerticalAlignment="Stretch" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ComboBox Grid.Row="0" Width="120" HorizontalAlignment="Left"></ComboBox>
            <ListBox HorizontalAlignment="Left" Name="listBox" Width="120" Grid.Row="1" <!--MaxHeight="500"--> />
        </Grid>         
    </ScrollViewer>

5 个答案:

答案 0 :(得分:2)

我知道这个问题已经过时了,但我遇到了完全相同的问题,并提出了一些修复方法,但它比没有解决问题更好。

我所阅读的大部分内容都表明在这种情况下使用像StackPanel这样的东西很糟糕,因为面板增长以适应它所拥有的元素。通常这样可以正常工作,因为您可以将StackPanel粘贴到网格中并设置列/行的MinHeight和MaxHeight并将控件锁定到位。一旦添加了ScrollView,这种情况就会变得很糟糕。上面的答案很好地描述了问题,但缺乏解决方案。

我尝试了许多不同类型的Panel而不是StackPanel,但它们都产生了相同的结果。我决定,因为我的ListBox位于Grid内部,我需要将该网格位置的MaxHeight绑定到控件中的其他值,以防止ListBox增长。这个问题是没有任何元素可以直接绑定到你想要的确切高度。

输入黑客:

我的身高只是一点点太大,造成一个奇怪的总屏幕外ListBox(事实上36像素太大,这是ListBox上方标签的高度)。所以我实现了IValueConverter:

class HeightToAdjustedHeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var height = (double) value - 33d;
        return height < 360d ? 360d : height;
        //360 being the minimum height allowed for the listbox
    }

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

}

之后我所做的只是将其作为MaxHeight绑定的转换器(注意你需要命名你的usercontrol并绑定到它的x:Name):

<Grid Grid.Column="0"
    Grid.Row="1"
    VerticalAlignment="Top"
    ClipToBounds="True"
    MaxHeight="{Binding ElementName=AdHocUserControl, Path=ActualHeight, Converter={StaticResource HeightToAdjustedHeightConverter}}">

我能想到的唯一另一种选择是扩展其中一个面板并试图发挥其增长行为。我承认这是一个黑客,但它会奏效。

答案 1 :(得分:1)

试试这个

<ScrollViewer HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto"  Name="scrollViewer1"  VerticalAlignment="Stretch" >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ComboBox Grid.Row="0" Width="120" HorizontalAlignment="Left" ></ComboBox>
            <ListBox HorizontalAlignment="Left"  Name="listBox"  Width="120" VerticalAllignment = "Top" Grid.Row="1"/>
        </Grid>

    </ScrollViewer>

或者你也可以试试这个

  <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ComboBox Grid.Row="0" Width="120" HorizontalAlignment="Left" ></ComboBox>
            <ListBox HorizontalAlignment="Left"  Name="listBox" VerticalAlignment= "Top"  Width="120" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
        </Grid>

答案 2 :(得分:1)

您的定义存在逻辑上的不一致。

你提出的要求:“我希望当所有信息都无法在屏幕上显示[不使用MaxHeight]时显示页面的滚动查看器” - 出现一个问题:“你如何确定'所有信息都无法在屏幕上显示'?“或“ListBox应该在什么时候停止增长并显示滚动条?”。

从WPF \ Silverlight布局管理逻辑中,它完全符合您的要求 - 当列表框的高度加上组合框的高度之和大于滚动查看器的ViewportHeight时,您将得到滚动条。只有当ListBox增长到没有滚动条的所需大小时,才有可能实现这一点。

答案 3 :(得分:0)

不要在ListBox上设置MaxHeight,只需在RowDefinitions中使用星号“*”表示法,即可正确显示两个控件之间的相对大小。

答案 4 :(得分:0)

另一种解决方案:

<ScrollViewer HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto" Name="scrollViewer1" VerticalAlignment="Stretch" >
<Grid>
   <Grid.RowDefinitions>
     <RowDefinition Height="Auto"/>
     <RowDefinition Height="*"/>
   </Grid.RowDefinitions>
 <ComboBox Grid.Row="0" Width="120" HorizontalAlignment="Left"></ComboBox>
 <ScrollViewer x:Name="scrollViewer" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
    <ListBox HorizontalAlignment="Left" Name="listBox" Width="120" Grid.Row="1" MaxHeight="{Binding ActualHeight, ElementName=scrollViewer}" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
  </ScrollViewer>
</Grid>