我如何随机列表框中的所有项目

本文关键字:项目 何随机 列表 | 更新日期: 2023-09-27 17:50:44

我不想随机编号,我想随机在c# XAML Windows Store应用程序的列表框中的所有项目??

谁有代码或者知道怎么做吗?

我有这个代码listbox
<ListBox Name="Playlist" Background="White" DoubleTapped="Playlist_DoubleTapped" KeyUp="Playlist_KeyUp"
     Foreground="Black" SelectionMode="Single" SelectionChanged="Playlist_SelectionChanged"
     ScrollViewer.VerticalScrollMode="Enabled" 
     ScrollViewer.VerticalScrollBarVisibility="Auto" 
     Grid.Column="0" Margin="28,82,1067,32">

@Baldrick这就是我如何将歌曲/视频添加到播放列表列表框

public async void OpenFileButton_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker filePicker = new FileOpenPicker();
    filePicker.ViewMode = PickerViewMode.List;
    filePicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
    filePicker.CommitButtonText = "Play";
    foreach (string fileExtension in supportedAudioFormats)
    {
        filePicker.FileTypeFilter.Add(fileExtension);
    }
    foreach (string fileExtension in supportedVideoFormats)
    {
        filePicker.FileTypeFilter.Add(fileExtension);
    }
    IReadOnlyList<StorageFile> selectedFiles = await filePicker.PickMultipleFilesAsync();
    if (selectedFiles.Count > 0)
    {
        foreach (StorageFile file in selectedFiles)
        {
            Playlist.Items.Add(file);
        }
        systemMediaControls.IsEnabled = true;
        mediaSource.AutoPlay = true;
        await SetNewMediaItem(0);   // Start with first file in the list of picked files.
        StorageFile files = Playlist.SelectedItem as StorageFile;
        Thumbnail(files);
    }
}

我如何随机列表框中的所有项目

下面是一个例子。

XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ListBox Grid.Row="0" Name="Playlist" ItemsSource="{Binding Path=PlaylistItems}"/>
    <Button Grid.Row="1" Name="Shuffle" Content="Shuffle" Click="Shuffle_Click"/>
</Grid>

注意PlaylistItemsListBox结合。最好是操作绑定到ListBox的项目集合,而不是操作ListBox本身。

c#代码:

首先应该定义PlaylistItems集合。

public ObservableCollection<string> PlaylistItems
{
    get;
    set;
}

并且为了使它能够绑定到ListBox,有必要设置控件(窗口或其他)的DataContext

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;  // <--
}

现在让我们初始化PlaylistItems集合并添加一些项。

public void InitializeItems(int count)
{
    var items = new List<string>(count);
    for (int i = 0; i < count; i++)
        items.Add("Item " + i);
    PlaylistItems = new ObservableCollection<string>(items);
}

可以在构造函数中调用这个方法。

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
    InitializePlaylist(10); // <--
}

最后是洗牌功能。

public IEnumerable<string> ShuffleItems(IEnumerable<string> items)
{
    var rnd = new Random();
    return items.OrderBy(item => rnd.Next()).ToList();
}

在按钮处理程序中使用它来用随机项重新填充PlaylistItems

private void Shuffle_Click(object sender, RoutedEventArgs e)
{
    var shuffledItems = ShuffleItems(PlaylistItems);
    PlaylistItems.Clear();
    foreach (var item in shuffledItems)
        PlaylistItems.Add(item);
}