WPF XAML 将 PositiveInfinity 返回为 DesiredSize 并带有充满活力的变形面板

WPF XAML returning PositiveInfinity as DesiredSize with vitalized warp panel

我需要为充满活力的变形面板实施第三方解决方案。由于此错误消息,我尝试的前两个不起作用:

' ...should not return PositiveInfinity as its DesiredSize, even if Infinity is passed in as available size.'

所以接下来我去了一个商业解决方案,它给了我完全相同的错误。很明显我的代码有问题。但无论我尝试什么,问题仍然存在。请注意,我用一个基本示例尝试了最后一个解决方案,它确实有效。

我认为我的一个控件有问题,可能需要规定尺寸,但是,我已经在每个相关控件上都试过了。下面是代码。它是使用数据模板的 Grid 内部的 ListBox。

<Grid x:Name="MainGrid" Background="#222">

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="300" MinWidth="200" MaxWidth="400" />
        <ColumnDefinition Width="*" MinWidth="200" />
        <ColumnDefinition Width="400" MinWidth="270" MaxWidth="600" />
    </Grid.ColumnDefinitions>

...

        <!--#region Main Library Column-->
    <ListBox x:Name="LibraryBox"
             Grid.Column="1" 
             Grid.Row="0"
             Grid.RowSpan="4"
             Margin="20,10,20,10"
             BorderBrush="Transparent"
             Background="#1e1e1e"

             ItemsSource="{Binding}" 
             ItemContainerStyle="{StaticResource ListBoxItemStyle}"
             IsSynchronizedWithCurrentItem="True"

             VirtualizingPanel.IsVirtualizing="True"
             VirtualizingPanel.IsVirtualizingWhenGrouping="True"
             VirtualizingPanel.IsContainerVirtualizable="True"
             VirtualizingPanel.CacheLengthUnit="Page"
             VirtualizingPanel.CacheLength="1,2"
             VirtualizingPanel.VirtualizationMode="Standard"
             VirtualizingPanel.ScrollUnit="Pixel"

             ScrollViewer.IsDeferredScrollingEnabled="False"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"

             SelectionMode="Extended"
             SelectionChanged="LibraryBox_SelectionChanged" 
             MouseDoubleClick="LibraryBox_MouseDoubleClick">
        <ListBox.Resources>
            <!--#region scrollbar style-->
            <Style TargetType="{x:Type ScrollBar}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Foreground" Value="#990000"/>

                <Setter Property="Width" Value="25"/>
            </Style>
            <!--#region Not working...-->
            <Style TargetType="{x:Type RepeatButton}">
                <Setter Property="Background" Value="AliceBlue"/>
                <Setter Property="Foreground" Value="Transparent"/>
                <Setter Property="Width" Value="40"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type RepeatButton}">
                            <Border Background="Red" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            <!--#endregion-->
            <!--#endregion-->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/>
        </ListBox.Resources>

        <ListBox.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Edit" Click="ContextMenuItemEdit_Click"/>
                <MenuItem Header="ComicVine Scraper" Click="MenuItemScraper_Click"/>
                <Separator/>
                <MenuItem Header="Delete" Click="ContextMenuItemDelete_Click"/>
            </ContextMenu>
        </ListBox.ContextMenu>

        <!--#region Group Style-->
        <ListBox.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True">
                                        <Expander.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" 
                                                                FontWeight="Bold" 
                                                                Foreground="#dbdbdb" 
                                                                FontSize="16" 
                                                                FontFamily="Cordia New"
                                                                VerticalAlignment="Bottom" />
                                                <TextBlock Text="{Binding ItemCount}" 
                                                                FontSize="16" 
                                                                Foreground="#dbdbdb" 
                                                                FontStyle="Italic" 
                                                                Margin="10,0,0,0" 
                                                                FontFamily="Cordia New"
                                                                VerticalAlignment="Bottom" />
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListBox.GroupStyle>
        <!--#endregion-->

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Viewbox Height="100" Width="70" Margin="2">
                        <Viewbox.LayoutTransform>
                            <ScaleTransform ScaleX="{Binding Value, ElementName=ZoomSlider}"
                                        ScaleY="{Binding Value, ElementName=ZoomSlider}"/>
                        </Viewbox.LayoutTransform>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="300"/>
                                <ColumnDefinition Width="32"/>
                            </Grid.ColumnDefinitions>
                            <!--This is for thumbnails-->
                            <Image x:Name="coverImage"
                                    Grid.Column="0"
                                    Grid.Row="0"
                                    Source="{Binding CoverPath, Converter={StaticResource UriToBitmapConverter}, IsAsync=True}"/>
                            <Rectangle x:Name="ReadMarkerBottom"
                                        Grid.Column="1"
                                        Grid.Row="1"
                                        Margin="-16,0,0,0"
                                        Fill="#fff"
                                        Width="32"
                                        Height="32"
                                        Loaded="CoverImage_Loaded"/>
                            <Rectangle x:Name="ReadMarkerTop"
                                        Grid.Column="1"
                                        Grid.Row="1"
                                        Margin="-16,0,0,0"
                                        Fill="#000"
                                        Width="30"
                                        Height="30"
                                        Loaded="CoverImage_Loaded"/>
                        </Grid>
                    </Viewbox>

                    <TextBlock TextTrimming="CharacterEllipsis"
                                TextAlignment="Center"
                                Width="120"
                                Foreground="#dbdbdb"
                                Background="Transparent"
                                Margin="0,0,0,5"
                                Loaded="Text_Loaded"
                                FontFamily="Cordia New"
                                FontWeight="Bold">
                    </TextBlock>
                    <TextBlock TextTrimming="CharacterEllipsis"
                                TextAlignment="Center"
                                Width="120"
                                Foreground="#dbdbdb"
                                Background="Transparent"
                                Margin="0,0,0,5"
                                Loaded="IssueNumer_Loaded"
                                FontFamily="Cordia New"/>
                    <TextBlock TextTrimming="CharacterEllipsis"
                                TextAlignment="Center"
                                Width="120"
                                Foreground="#dbdbdb"
                                Background="Transparent"
                                Margin="0,0,0,5"
                                Loaded="CountStack_Loaded"
                                FontFamily="Cordia New"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <extendedPanelControls:BinaryVirtualizingWrapPanel 
                    AutoUpdateViewportOnDetectingTransactionalUpdatesToDataSource="True"
                    ItemPositioningMode="Greedy"
                    IsItemsHost="True"
                    Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
    <!--#endregion-->

答案是在 GroupStyle 内的 <ItemsPresenter /> 上设置 MaxHeight,这样它就不会永远持续下去了