2011-05-18 147 views
5

在我们的应用程序中,我们有一些ListViews和ListBoxes内部网格,您可以通过网格分隔器帮助更改控件的实际高度。 这样做时,您可以安排ListBox的高度,因此其中一个项目不完全可见,因为ListView变得很短以显示它。防止WPF列表视图或列表框显示“一半”项目

这是我们不想要的行为。

从我的研究到目前为止似乎没有办法阻止ListBox或ListView显示部分项目,但也许有人找到了另一种方法来处理这个问题。也许物品只有一半可见时才会触发自己。但是我们怎么能发现?

我们欢迎任何建议。

+0

你为什么要防止显示部分项目?只要列表是可滚动的,这对人们来说就是内容可以滚动的一个很好的指示器。 – 2011-05-18 19:45:02

回答

1

我不知道用这种方法修复ListView。不过,对于ListBox,您可以重写ArrangeOverride并自己排列项目。堆叠您想要查看的项目,并排列您不希望看到的项目(例如,部分可见项目),以便它们不可见。例如,非虚拟化版本:

/// <summary> 
    /// Used to arrange all of the child items for the layout. 
    /// Determines position for each child. 
    /// </summary> 
    /// <param name="finalSize">Parent passes this size in</param> 
    /// <returns>Parent size (always uses all of the allowed area)</returns> 
    protected override Size ArrangeOverride(Size finalSize) 
    { 
     // Arrange in a stack 
     double curX = 0; 
     double curY = 0; 
     foreach (UIElement child in InternalChildren) 
     { 
      double nextY = curY + child.DesiredSize.Height; 
      if (nextY > finalSize.Height)   // Don't display partial items 
       child.Arrange(new Rect()); 
      else 
       child.Arrange(new Rect(curX, curY, child.DesiredSize.Width, child.DesiredSize.Height); 
      curY = nextY; 
     } 

     return finalSize; 
    } 
0

你只需要或了minHeight设置MinWidth到找你调整的单元格。试试这个:

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition MinHeight="50"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition MinHeight="50"/> 
     </Grid.RowDefinitions> 
     <ListBox HorizontalAlignment="Center" Margin="10"> 
      <ListBoxItem>First</ListBoxItem> 
      <ListBoxItem>Second</ListBoxItem> 
      <ListBoxItem>Third</ListBoxItem> 
      <ListBoxItem>Fourth</ListBoxItem> 
     </ListBox> 
     <GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 
     <ListView Grid.Row="2" Margin="4"> 
      <ListViewItem>First</ListViewItem> 
      <ListViewItem>Second</ListViewItem> 
      <ListViewItem>Third</ListViewItem> 
      <ListViewItem>Fourth</ListViewItem> 
     </ListView> 
    </Grid>