繁体   English   中英

C#WPF-如何在ListView中具有复选框?

[英]C# WPF - How to Have CheckBoxes in a ListView?

我目前正在C#WPF中创建MP3播放器。 我的MP3播放器的侧面有一个ListView,它显示了当前的完整播放列表,允许用户选择一首歌曲,或者让MP3播放器滚动浏览这些歌曲。 我目前正在尝试实现以下功能:

每个项目旁边都有一个复选框。 当前歌曲结束后,如果下一首歌曲被选中,则播放它,否则跳过它。

我已经在广泛搜索中找到了适合我的答案,但是我似乎找到的只是对我来说没有意义的答案,因此我无法实现它们以适合我的程序。

我什至不知道如何去实现这个功能。

由于歌曲是从openFileDialog加载的,因此无法找到以编程方式将CheckBox添加到ListView中的每个项目的方法。

此外,我曾尝试使用Wpf Extended Toolkit的CheckListBox控件,但这并不适合,因为该控件的许多事件和/或属性与ListView的事件和/或属性不同,从而使程序的某些功能无法使用,例如作为“还原歌曲更改”(将上一首歌曲播放到用户更改它的时间),或用于加载与程序关闭时相同的歌曲的功能。

如果有人可以引导我给出答案,或者以简单的方式向我解释这个问题,将不胜感激。

谢谢您的帮助。

你好! 就像我在帖子评论中所说的那样, 我实际上找到了解决方案,这要归功于Rufo爵士给我提供了有关项目模板的线索 ,以及KettuJKL的回答。

对于通过这篇文章寻找相同答案的任何人:

为了在C#WPF中使用CheckBoxesTextBlocks和诸如此类的东西来创建ListView ,您必须(可以)采取一些简单的步骤:

首先,.cs文件中创建一个该类具有获取/设置 ListView所需的每个控件的值的 属性 下面显示了我的Media Player中的这种用法示例

 public class Song //Class name - could be anything, so long as it suits you
 {
    public bool Favourite { get; set; } //Value for CheckBox #1 - whether it is a favourite
                                        //(true - checked) or not (false - unchecked)
    public bool Play { get; set; } //Value for CheckBox #2 - whether the song is played
                                   //when its turn is reached (true - play/false - skip)

    public string Name { get; set; } //A string value of the name of the song
 }

其次,使用XAML将有问题的ListView添加到您的程序中。 使用以下代码创建ListView:

<ListView Grid.Row="2" HorizontalAlignment="Stretch" Name="MyListView"
Margin="5" SelectionChanged="SelectedSongChanged" Grid.RowSpan="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Favourite">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Margin="5, 0" IsChecked="{Binding Favourite}"/>
                                <!-- Your control type and value goes here. Bind the value
                                     to the name of the method that represents this value
                                     in your .cs file. In my case, this was 'Favourite' -->
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Play">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Margin="5, 0" IsChecked="{Binding Play}"/>
                                <!-- Repeat this for every control you need in your
                                     ListView's rows -->
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Margin="5, 0" Text="{Binding Name}"/>
                                <!-- You can add non-binded values too to each column.
                                     These values will simply be added to every row
                                     and be the same. -->
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

最后(也是最后一个),您无需向列表视图中直接添加新条目而需要向列表中添加项目,该列表将每一行的值存储为新条目。 每当需要更新ListView时,都需要将列表设置为ListView的ItemsSource。 如下所示:

private void AddSong()
{
    List<Song> playlistSongs = new List<Song>(); //Set a list that holds values of your
                                                 //class' type. If you made a class
                                                 //called 'MyClass', your list would
                                                 //be List<MyClass> instead.

    playlistSongs.Add(new Song() { Favourite = false, Play = true, Name = "Song 1");
    //Add a new song to the list of songs. Each value in the class is represented
    //as above. You can change these values to be different, or even calculated
    //depending on variables.

    MyListView.ItemsSource = playlistSongs; //Finally, set each row's values in your
                                            //ListView equivalent to each entry
                                            //in your list created earlier.

}

完蛋了!

这就是创建ListView所需的全部内容,其中的列表示不同的值并包含不同的控件。

StackOverflow在这里处理ListView CheckBoxes有多个问题。 看到其中一些

可以从MSDN中找到简单的示例。

简而言之:

  1. 您需要包含复选框的DataTemplate。
  2. 您需要将该复选框绑定到属性以操纵其值。

我希望您使用的是MVVM,它将有助于绑定复选框属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM