我需要在两个 ListBox 列中显示两个列表,但是通过 MVVM 模式它会是一行。我已经搜索了互联网,但还没有找到任何东西。我只是在学习,也许正因为如此我错过了一些东西,如果有人有机会解释,或者至少是某种例子,我会很高兴。
视图模型:
private List<string> _artists = new();
private List<string> _names = new();
private List<Song> _songs = new();
public List<string> Artists
{
get
{
return _artists;
}
set { _artists = value; OnProopertyChanged(); }
}
public List<string> Names
{
get
{
return _names;
}
set { _names = value; OnProopertyChanged(); }
}
public List<Song> Songs
{
get
{
return _songs;
}
set { _songs = value; OnProopertyChanged(); }
}
for (int counterForList = 1; counterForList < 41; counterForList++)
{
HtmlMethod(counterForList,2, out Artist);
HtmlMethod(counterForList,3, out Name);
Song song = new Song(Name, Artist);
Songs.Add(song);
}
foreach (var song in Songs)
{
Artists.Add(song.Artist);
Names.Add(song.Name);
}
模型:
public class Song
{
public string Name { get; set; }
public string Artist { get; set; }
和 MainWindow 本身:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:AvaloniaApplication1.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="350"
x:Class="AvaloniaApplication1.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="AvaloniaApplication1"
Width="600"
Height="350">
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1"/>
<ListBox Items="{Binding Songs}" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" Grid.RowSpan="2">
<ListBox.ItemTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
这里是代码,我不明白如何在不同的列中显示艺术家和名称,或者可以划分歌曲。在 avalonia 纪录片中写道,他们没有 ItemSource,他们只有一个 Item。