你好!如我在帖子评论中所说,我实际上通过Sir Rufo给我的关于项目模板的建议以及KettuJKL的回答找到了解决方案。
对于任何查找相同答案的人:
为了在C# WPF中创建一个包含复选框、文本块和其他控件的ListView,您需要采取几个简单的步骤:
首先,在.cs文件中创建一个类,其中属性可以获取/设置每个控件的值,这些值是您的 ListView 需要的。下面以我的媒体播放器为例展示了此用法:
public class Song
{
public bool Favourite { get; set; }
public bool Play { get; set; }
public string Name { get; set; }
}
其次,使用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时,您都需要将该列表设置为ListView的ItemsSource。如下所示:
private void AddSong()
{
List<Song> playlistSongs = new List<Song>();
playlistSongs.Add(new Song() { Favourite = false, Play = true, Name = "Song 1");
MyListView.ItemsSource = playlistSongs;
}
完成了!
这就是你需要创建一个ListView的全部内容,它表示不同值和控件的列。