A T*_*A T 2 c# wpf checkbox listview
我目前正在用 C# WPF 创建 MP3 播放器。我的 MP3 播放器侧面有一个 ListView,它显示完整的当前播放列表,允许用户选择歌曲,或让 MP3 播放器滚动浏览歌曲。我目前正在尝试实现以下功能:
每个项目旁边都有一个复选框。当前歌曲播放完毕,如果选中下一首歌曲,则播放,否则跳过。
我已经广泛搜索了适合我的答案,但是我似乎找到的只是对我没有意义的答案,因此我无法实现它们以适合我的程序。
我什至不知道如何实现这个功能。
由于歌曲是从 openFileDialog 加载的,我找不到以编程方式将 CheckBoxes 添加到 ListView 中的每个项目的方法。
此外,我曾尝试使用 Wpf Extended Toolkit 的 CheckListBox 控件,但是这并不合适,因为该控件的许多事件和/或属性与 ListView 的不同,导致该程序的某些功能无法使用,例如作为“还原歌曲更改”(将上一首歌曲播放到用户更改它的时间)或加载与程序关闭时相同歌曲的功能。
如果有人能引导我找到答案,或以简单的方式向我解释这一点,我将不胜感激。
谢谢您的帮助。
你好!正如我在帖子的评论中所说,我实际上找到了解决方案,这要归功于Rufo 爵士给我提供的有关项目模板的信息,以及KettuJKL的回答。
对于通过这篇文章寻找相同答案的任何人:
为了在C# WPF 中使用CheckBoxes、TextBlocks和诸如此类的东西创建ListView,您必须(可以?)采取一些简单的步骤:
首先,创建一个类在你的cs文件与性能是获取/设置每一个的控件的值,你的ListView的需求。下面显示了我的媒体播放器中这种用法的示例:
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
}
Run Code Online (Sandbox Code Playgroud)
其次,使用 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>
Run Code Online (Sandbox Code Playgroud)
最后(也是最后),不是直接向 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.
}
Run Code Online (Sandbox Code Playgroud)
并完成了!
这就是创建一个 ListView 所需的全部内容,其中的列代表不同的值并持有不同的控件。
| 归档时间: |
|
| 查看次数: |
11696 次 |
| 最近记录: |