Prevent WPF ListView or ListBox from showing “half” Items

元气小坏坏 提交于 2019-12-06 03:24:57

问题


in our application we have some ListViews and ListBoxes inside grids where you can change the actual height of the control with help of a grid splitter. When doing so you are able to arrange the height of the ListBox so one of the items is not fully visible because the ListView becomes to short to display it.

This is a behavior we don't want.

From my research so far it seems there is not way the prevent a ListBox or ListView from showing partial items but maybe someone found another way to deal with this problem. Maybe the item can trigger itself invisible when it is only half visible. But how can we find out?

We are open to any suggestions.


回答1:


There's no way I know of to fix a ListView this way. For ListBox, though, you can override ArrangeOverride and arrange the items yourself. Stack the items you want to see, and arrange the items you do not wish to be visible (e.g., partially visible items) so that they are not visible. For example, a non-virtualized version:

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



回答2:


You just have to set MinHeight or MinWidth to the cell which you'r resizing. Try this:

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


来源:https://stackoverflow.com/questions/6045299/prevent-wpf-listview-or-listbox-from-showing-half-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!