阻止列表框显示部分Listboxitem

时间:2015-01-25 16:18:50

标签: c# wpf listbox

我有一个显示信息的非交互式UI。信息保存在具有自定义listboxitem控件的Listbox控件中。

每个listboxitem的大小可以变化,窗口可以根据用户的要求调整大小。

我想阻止部分显示在应用程序中的listboxitems显示,这将始终是最后一行。但是,如果我调整用户界面的大小,那么最后一行'将改变。

我一直在试验this SO answer而没有任何运气。

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;

        }

" InternalChildren"我假设是我的列表框,但Listbox不可枚举。我不确定如何使用这个例子。

如何达到预期效果?

主要控制

<UserControl x:Class="Cis.CustomControls.CisDeparturesPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:customControls="clr-namespace:Cis.CustomControls"
             mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800">
    <UserControl.Resources>
        <DataTemplate x:Key="DataTemplate">

            <ListBoxItem Padding="0"  >
                <customControls:CisDeparturesRow />
            </ListBoxItem>

        </DataTemplate>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="48"/>
            <RowDefinition Height="44"/>
            <RowDefinition Height="229*"/>
            <RowDefinition Height="279*" />
        </Grid.RowDefinitions>


        <ListBox Grid.Row="2" HorizontalContentAlignment="Stretch" Name="ListBoxDepart" Background="Black" Margin="1,0,-1,0" BorderThickness="0" ItemsSource="{Binding Path=StationMovementList}" ItemTemplate="{StaticResource DataTemplate}" ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Hidden" Grid.RowSpan="2">
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Focusable" Value="False"/>
                </Style>
            </ListBox.ItemContainerStyle>

        </ListBox>


    </Grid>
</UserControl>

ListBoxItem自定义控件

<UserControl x:Class="Cis.CustomControls.CisDeparturesRow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="80" d:DesignWidth="800" Background="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}, Path=Background}" HorizontalContentAlignment="Stretch" Margin="0,0,0,0" Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}, Path=ActualWidth}">
    <UserControl.Resources>
<!-- snip --->
</UserControl.Resources>
    <Grid x:Name="ArrivalRowGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="47*"/>
            <RowDefinition Height="33*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="141"/>
            <ColumnDefinition Width="117"/>
            <ColumnDefinition Width="241"/>
        </Grid.ColumnDefinitions>

        <TextBlock Name="TxtDestinationName" MouseDown="TxtDestinationName_OnMouseDown" Style="{StaticResource MainRowStyle}"  TextWrapping="Wrap" Text="{Binding Path=DestinationName,NotifyOnTargetUpdated=True, Mode=OneWay}" Margin="10,0"/>
        <TextBlock Name="TxtPlatNum" TextWrapping="Wrap" Style="{StaticResource MainRowStyle}" Text="{Binding Path=Platform,NotifyOnTargetUpdated=True, Mode=OneWay}" Margin="11,0,8,0"
                   Grid.Row="0" Grid.Column="1"/>

        <TextBlock Name="TxtArrTime" TextWrapping="Wrap" Style="{StaticResource MainRowStyle}" TextAlignment="Right" Text="{Binding Path=ExpectedDepartureTime,NotifyOnTargetUpdated=True, Mode=OneWay}" Margin="10,0,21,8"
                   Grid.Row="0" Grid.Column="3" />

        <TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4" x:Name="TxtCallingAt" TextWrapping="Wrap" Style="{StaticResource SubRowStyle}" TextAlignment="Left" Text="{Binding Path=CallingAt,NotifyOnTargetUpdated=True, Mode=OneWay}" Margin="10,0,21,0" />

    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

您链接的SO问题包含ArrangeOverride方法,该方法适用于自定义Panel,而不是ListBox本身。

WPF ListBox控件的模板,一旦展开,看起来就像这样(当然简化):

<Border>
    <ScrollViewer>
        <SomePanel>
            <ListBoxItem>your item</ListBoxItem>
            <ListBoxItem>your item</ListBoxItem>
            <ListBoxItem>your item</ListBoxItem>
            <ListBoxItem>etc</ListBoxItem>
        </SomePanel>
    </ScrollViewer>
</Border>

其中SomePanel是从Panel派生的类的实例。列表框的默认值是VirtualizingStackPanel,但可以像这样替换:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            your panel type here
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

你在问题​​中说这是一个“非交互式用户界面”。这有几种不同的解释,但暗示我可以用ListBox替换ItemsControl,因为您不需要列表框的选择功能。然后创建一个自定义面板(一个派生自Panel的类),从链接的SO问题中添加ArrangeOverride方法,并将ItemControl面板替换为自定义面板。