2010-10-18 97 views
0

给定以下XMAL为什么没有垂直滚动条用于绑定到100个字符串的ObservableCollection的ListBox。如果我改变了第二行的高度从*到一些固定的像500,然后会出现一个滚动条,但很明显,我想行高是什么都可以(这是我的理解*的意思)为什么没有此列表框的垂直滚动条

<UserControl x:Class="SimpleStack.MainPage" 
    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" 
    d:DesignHeight="300" d:DesignWidth="400"> 
    <UserControl.Resources> 
     <DataTemplate x:Key="ListBoxItemTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="Place holder"/><TextBlock Text="{Binding}"/> 
      </StackPanel> 
     </DataTemplate> 
    </UserControl.Resources> 
    <Grid x:Name="LayoutRoot" Background="Azure"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <TextBlock Text="The Text" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/> 
     <ListBox ItemsSource="{Binding ListOfNumbers}" Grid.Row="1" Grid.Column="0" 
       ItemTemplate="{StaticResource ListBoxItemTemplate}"/> 
     <TextBlock Text="Place Holder" Grid.Row="1" Grid.Column="1"/> 
    </Grid> 
</UserControl> 

回答

1

*行高事实上“其他所有可用”(如果你有多个* s,它会把它分开)。我猜你的实际问题是“无论什么”都是无限的。用户控件很可能会被赋予无限的空间,因此它正在扩展以占用尽可能多的空间。确保你限制你的用户控件到实际的可见空间,你的列表框应该得到它的滚动条。

+0

它确实显示我的UserControl正在被给予无限的空间,正如你所建议的那样。 Howerver我不知道为什么会这样?插件获得100%x100%(VS测试页面的SOP) – 2010-10-18 16:18:57

+0

是否有一种工具可以在运行时调查布局,类似于IE开发人员工具? – 2010-10-18 16:20:58

+1

你的UserControl是应用程序的RootVisual还是包含在另一个页面中?如果它包含在另一个页面中,请检查以确保其包含的面板不是“StackPanel”,这会导致它具有无限可用大小。 – Stephan 2010-10-18 16:43:14

1

我的理解是,由于度量/排列布局系统,您基本上可以告诉列表框,它可以拥有所需的所有垂直空间而不受约束。因此,默认列表框模板中的内部ScrollViewer从不受限制触发滚动条出现。

我可以看到两种方法来解决这个问题您的具体情况:

- 指定ScrollViewer.VerticalScrollBarVisibility="Visible"在列表框,迫使内部的ScrollViewer始终显示滚动条。

- 使用一个实际的ScrollViewer包含列表框,并让提供滚动能力,而不是一个内部列表框(您可能需要调整填充和边框让它右看看):

<ScrollViewer Grid.Row="1" Grid.Column="0"> 
    <ListBox ItemsSource="{Binding ListOfNumbers}" 
      ItemTemplate="{StaticResource ListBoxItemTemplate}"/> 
</ScrollViewer> 

我更喜欢第二种方式,因为如果真的有必要,它只会显示垂直滚动条。