1
我不知道根据MVVM显示的控件数据。MVVM的问题。动态的网格列表
我有一辆车的集合。
我希望按类型分组(例如,轿车,组合,掀背车),并取决于打印网格类型的数量。
所以:
5辆: 2×轿车,2×的Combi,1×sportcar。
所以我想打印3个网格。
它如何与MVVM保持一致。
我不知道根据MVVM显示的控件数据。MVVM的问题。动态的网格列表
我有一辆车的集合。
我希望按类型分组(例如,轿车,组合,掀背车),并取决于打印网格类型的数量。
所以:
5辆: 2×轿车,2×的Combi,1×sportcar。
所以我想打印3个网格。
它如何与MVVM保持一致。
下面是一些示例代码。如果您的汽车列表可以改变,您应该使用ObservableCollections代替或在您的视图模型上实现INotifyPropertyChanged。 XAML:
<Window x:Class="TestApp.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid>
<ListBox ItemsSource="{Binding Path=CarTypes}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Key}" />
<ListBox ItemsSource="{Binding Path=Value}" DisplayMemberPath="Name" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
</Window>
后面的代码:
using System.Collections.Generic;
using System.Windows;
namespace TestApp
{
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
DataContext = new CarsVM();
}
}
public class CarsVM
{
public CarsVM()
{
CarTypes = new Dictionary<string, List<Car>>();
// You want to populate CarTypes from some model.
CarTypes["sedan"] = new List<Car>() {new Car("Honda Accord"), new Car("Toyota Camry")};
CarTypes["musclecar"] = new List<Car>() { new Car("Chevy Camaro"), new Car("Dodge Challenger") };
CarTypes["suv"] = new List<Car>() { new Car("Chevy Tahoe") };
}
public Dictionary<string, List<Car>> CarTypes { get; private set; }
}
public class Car
{
public Car(string name)
{
Name = name;
}
public string Name { get; set; }
}
}
非常感谢你:) – user278618 2010-06-26 09:14:43