带有数据绑定分层数据的WPF TreeView

时间:2012-03-04 04:57:46

标签: treeview hierarchicaldatatemplate

我是MVVM和WPF方法的新手,并尝试使用它开发业务应用程序。我最终得到了以下代码结构来实现具有多个类型节点的树视图

我在用户控件中定义了一个树视图,如下所示:

<Grid>
    <Grid.Resources>           
        <DataTemplate DataType="{x:Type vm:ProductViewModel}" x:Key="ProductTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type vm:ClientViewModel}"
                 ItemTemplate="{StaticResource ProductTemplate}" 
                                  x:Key="ClientTemplate" 
                                  ItemsSource="{Binding Products}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </HierarchicalDataTemplate>
    </Grid.Resources>        
    <TreeView Name="treeView1" Margin="0,0,0,0" 
              Loaded="treeView1_Loaded" 
              SelectedItemChanged="treeView1_SelectedItemChanged">
        <TreeViewItem Header="Majestic" Name="rootNode" ItemsSource="{Binding Clients}" 
                      ItemTemplate="{StaticResource ClientTemplate}" >
        </TreeViewItem>
    </TreeView>
</Grid>

树视图的Datacontext在代码隐藏文件中设置:

public TreeCtrlView()
    {
        InitializeComponent();
        treeView1.DataContext = new ViewModels.TreeViewModel();
    }

数据模型如下:

class TreeViewModel : ViewModelBase
{
    public ObservableCollection<ClientViewModel> Clients;

    public TreeViewModel()
    {
        var v = from client in DataModel.GetAllClients() select new ClientViewModel(client);

        Clients = new ObservableCollection<ClientViewModel>(v.ToList());
    }
}
class ClientViewModel : ViewModelBase
{
    private Client _client;
    private List<ProductViewModel> _products;

    public string Name { get { return _client.ClientName; } }
    public string Address { get { return _client.Address; } }
    public string City { get { return _client.City; } }
    public string State { get { return _client.State; } }
    public string PinCode { get { return _client.PinCode; } }
    public string ServiceTaxNumber { get { return _client.ServiceTaxNumber; } }
    public string ContactPerson { get { return _client.ContactPerson; } }
    public string ContactNumber { get { return _client.ContactNumber; } }
    public List<ProductViewModel> Products { get { return _products; } }

    public ClientViewModel(Client client)
    {
        _client = client;
        _products = new List<ProductViewModel>(from product in DataModel.GetClientProducts(client)
                                               select new ProductViewModel(product));
    }
}

class ProductViewModel : ViewModelBase
{
    Product _product;

    public long ID { get { return _product.ProductID; } }
    public string Name { get { return _product.Name; } }
    public decimal? Price { get { return _product.MRP; } }

    public ProductViewModel(Product product)
    {
            _product = product;
    }
}

不幸的是,除了没有+符号的Majestic节点外,下拉列表中没有任何内容。

1 个答案:

答案 0 :(得分:-1)

分层数据绑定问题的解决方案如下:

public class Order {
    private readonly string _instrument;
    private readonly double _price;
    private readonly long _quantity;

    public Order(string instrument, double price, long quantity) {
        _instrument = instrument;
        _price = price;
        _quantity = quantity;
    }

    public string Instrument {
        get { return _instrument; }
    }

    public double Price {
        get { return _price; }
    }

    public long Quantity {
        get { return _quantity; }
    }

    public void PopulateGrid(Grid grid) {
        BindingList<Order> orders = new BindingList<Order>();

        orders.Add(new Order("Instrument1", 10.55, 34));
        orders.Add(new Order("Instrument2", 12.26, 154));
        orders.Add(new Order("Instrument1", 13.16, 14));
        orders.Add(new Order("Instrument5", 9.85, 52));
        orders.Add(new Order("Instrument1", 16.47, 11));

        grid.DataSource = orders;
    }
}