gpt4 book ai didi

c# - 在 C# 中对包含网格控件的列表框进行排序

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:51 27 4
gpt4 key购买 nike

我正在编写一个应用程序,该应用程序将显示几个 jpeg 缩略图及其下方的文件名。我想按文件名对这些进行排序。这些 jpeg 文件来自一个 zip 文件,我无法按排序顺序接收它们。我正在使用这样定义的列表框:

   <ListBox Name="listPanel1" ScrollViewer.HorizontalScrollBarVisibility="Disabled"  ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionMode="Multiple" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Name="wrapPanel1" IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<TextBox Height="152" Name="tb_Messages" Width="244" />
</ListBox>

然后在代码中,我为每个缩略图添加了一个单独的网格控件到 listPanel。网格的第一行是图像,第二行是文件名。

        Grid grid = new Grid();
ColumnDefinition col0 = new ColumnDefinition();
RowDefinition row0 = new RowDefinition();
RowDefinition row1 = new RowDefinition();
col0.Width = new GridLength(140);
row0.Height = new GridLength(140);
grid.ColumnDefinitions.Add(col0);
grid.RowDefinitions.Add(row0);
grid.RowDefinitions.Add(row1);
grid.Children.Add(thumbnailImage);
grid.Children.Add(lb);
Grid.SetRow(thumbnailImage, 0);
Grid.SetColumn(thumbnailImage, 0);
Grid.SetRow(fileName, 1);
Grid.SetColumn(fileName, 0);
listPanel1.Items.Add(grid);

此方法的优点之一是,当我选择图像时,图像和文件名都会突出显示。

如何根据文件名对列表框进行排序?

这是我的第一个 WPF 应用程序,所以我完全有可能以完全错误的方式处理它。

最佳答案

不要在代码中创建用户界面!除非您正在创建用户控件。

  1. 使用 ListBox 并将其数据源绑定(bind)到表示图片的对象集合

    <ListBox ItemSource="{Binding Pictures}"/>
  2. 在您的 View 模型中,您将获得名称和其他属性,Pictures 属性将返回已排序的集合(或过滤后的集合或其他)

    public IEnumerable<Picture> Pictures
    {
    get { return _picturesLoadedFromZip.OrderBy(whatever); }
    }
  3. 要显示缩略图和文件名,请使用 template .

    <ListBoxItem Background="LightCoral" Foreground="Red" 
    FontFamily="Verdana" FontSize="12" FontWeight="Bold">
    <StackPanel Orientation="Horizontal">
    <Image Source="{Binding PathToFile}" Height="30"></Image>
    <TextBlock Text="{Binding FileName}"></TextBlock>
    </StackPanel>
    </ListBoxItem>

有关此的更多信息,您可以找到 herehere .

关于c# - 在 C# 中对包含网格控件的列表框进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12435634/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com