2012-03-04 102 views
0

我是MVVM和WPF方法學的新手,試圖開發使用它的業務應用程序。我已經結束了與下面的代碼結構,以實現與多個類型的節點帶有數據綁定分層數據的WPF TreeView

樹視圖我有一個用戶控制限定的樹形視圖如下:

<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

解決您的問題,分層數據綁定是在這裏:

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; 
    } 
}