より良いアプローチは、ItemsSource をデータ モデル Car タイプの新しいクラスの ObservableCollection にバインドすることです。
あなたの見解
.xaml
<StackPanel>
<ListBox ItemsSource="{Binding DataCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" - "/>
<TextBlock Text="{Binding Id}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
あなたのモデル
Car.cs
public class Car
{
public string Name { get; set; }
public int Id { get; set; }
}
ビュー モデルには、ItemsSource にバインドされるコレクションがあります。
CarViewModel.cs
public ObservableCollection<Car> DataCollection { get; set; }
DataCollection = new ObservableCollection<Car>
{
new Car { Name = "VW Golf GTI", Id = 30000 },
new Car { Name = "Porsche GT3", Id = 30000 },
new Car { Name = "Porsche Cayenne", Id = 80000 },
new Car { Name = "BMW M6", Id = 90000 }
};