我有一个显示信息的非交互式UI。此信息保存在具有自定义listboxitem控件的Listbox控件中。
每个列表框项的大小都可能不同,可以根据用户的需要调整窗口的大小。
我希望防止应用程序中部分显示的列表框项目显示,这将始终是最后一行。但是,如果我调整UI的大小,“最后一行”将发生变化。
我一直在用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“是我的列表框,但是列表框是不可枚举的。我不知道怎么用这个例子。
怎样才能达到预期的效果?
主控
<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>发布于 2015-01-26 16:32:34
链接到的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的面板替换为您的自定义面板。
https://stackoverflow.com/questions/28138528
复制相似问题