WPF VirtualizingStackPanel 排序后文本换行问题

问题描述 投票:0回答:1

我是 WPF 新手,编写此程序是为了帮助组织和编目我的书籍。我使用了虚拟化包裹面板,在我对书籍进行排序之前,该面板可以完美显示。

这是预排序的视图:预排序的图书列表

这里是查看帖子排序:排序书籍列表

这是 DataTemplate 的 XAML:

<!-- DataTemplate for GridView -->
<DataTemplate x:Key="GridViewTemplate">
    <ListView ItemsSource="{Binding FilteredBooks}"
          SelectedItem="{Binding SelectedBook, Mode=TwoWay}"
          SelectionChanged="OnBookSelected"
          VirtualizingStackPanel.IsVirtualizing="True" 
          VirtualizingStackPanel.VirtualizationMode="Recycling">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <virtualizingWrapPanel:VirtualizingWrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Width="160" Margin="5" ToolTip="{Binding Title}">
                <Image Source="{Binding CoverImage}" Height="240" Width="160" Stretch="UniformToFill" Margin="0,0,0,5"/>
                <TextBlock Text="{Binding Title}" TextAlignment="Center" FontWeight="Bold" TextWrapping="Wrap" />
                <TextBlock Text="{Binding Author}" TextAlignment="Center" TextWrapping="Wrap" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

这是我用来对列表进行排序的函数。

public async Task FilterAndSortBooks()
{
    var result = await Task.Run(() =>
    {
        IEnumerable<Book> filtered = Books;

        // Apply filter based on the selected filter option
        if (!string.IsNullOrWhiteSpace(SearchText))
        {
            filtered = SelectedFilter switch
            {
                "Title" => filtered.Where(book => book.Title.Contains(SearchText, StringComparison.OrdinalIgnoreCase)),
                "Author" => filtered.Where(book => book.Author.Contains(SearchText, StringComparison.OrdinalIgnoreCase)),
                "Release Year" => filtered.Where(book => book.Year.HasValue && book.Year.Value.ToString("yyyy").Contains(SearchText, StringComparison.OrdinalIgnoreCase)),
                _ => filtered
            };
        }

        // Apply sorting based on the selected sort option
        filtered = SelectedSortOption switch
        {
            "Title" => filtered.OrderBy(book => book.Title),
            "Author" => filtered.OrderBy(book => book.Author),
            "Release Year" => filtered.OrderBy(book => book.Year),
            _ => filtered
        };

        return filtered.ToList();
    });

    // Update FilteredBooks on the UI thread
    Application.Current.Dispatcher.Invoke(() =>
    {
        FilteredBooks.Clear();
        foreach (var book in result)
        {
            FilteredBooks.Add(book);
        }
    });
}

我希望排序的视图与预排序的视图相匹配,但无法弄清楚它为什么会改变。任何帮助都会很棒。

c# wpf word-wrap
1个回答
0
投票

查看“WrapPanel”中的第一项(第一张图片)。与它后面的所有项目相比,它的“大小”如何?是大了还是小了?

在没有明确的“项目”高度或宽度的情况下,集合中的“第一个”项目(尺寸)将驱动所有后续项目的“所需尺寸”。

因此,请确保所有物品的尺寸相同;或者,先大的;或者,使用特定的宽度和/或高度。

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.wrappanel.itemheight?view=windowsdesktop-8.0

© www.soinside.com 2019 - 2024. All rights reserved.