2010-07-02 219 views
1

我正在使用WCF和MVVM模式來填充樹視圖控件,我需要選擇的項目作爲視圖模型中的另一個方法的參數傳遞(以填充不同的控件)。WPF MVVM TreeView選擇的項目不填充當前選擇對象

treeview填充得很好,但選定的值沒有被傳遞給視圖模型。例如在視圖模型:

private ICollectionView m_SuppliersView; 
    public ObservableCollection<SupplierItem> SupplierItems 
    { 
     get 
     { 
      return supplierItems; 
     } 
     private set 
     { 
      if (supplierItems == value) 
      { 
       return; 
      } 
      supplierItems = value; 
      OnPropertyChanged("SupplierItems"); 
     } 
    } 
    public SupplierItem CurrentSupplier 
    { 
     get 
     { 
      if (m_SuppliersView != null) 
      { 
       return m_SuppliersView.CurrentItem as SupplierItem; 
      } 

      return null; 
     } 
    } 
    private void OnCollectionViewCurrentChanged(object sender, EventArgs e) 
    { 
// view model is inherited from a base class. base method listed below 
     OnPropertyChanged("CurrentSupplier"); 
    } 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     VerifyPropertyName(propertyName); 

     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      var e = new PropertyChangedEventArgs(propertyName); 
      handler(this, e); 
     } 
    } 

    private void Load() // Load event to populate the treeview source object 
    { 
// SupplierItems object is populated just fine and treeview displays just fine so I won't show how the sausage is made. I believe the issue is here: 
     m_SuppliersView = CollectionViewSource.GetDefaultView(SupplierItems); 

     if (m_SuppliersView != null) 
     { 
      m_SuppliersView.CurrentChanged += OnCollectionViewCurrentChanged; 
     } 

     OnPropertyChanged("CurrentSupplier"); 
在XAML

<Window.Resources> 
     <HierarchicalDataTemplate x:Key="SuppiersDistributorsTemplate" ItemsSource="{Binding Children}"> 
      <TextBlock Text="{Binding ManagedLocationName}"/> 
     </HierarchicalDataTemplate> 
</Window.Resources> 


<TreeView x:Name="tvSuppliers" ItemsSource="{Binding SupplierItems}" 
      ItemTemplate="{StaticResource SuppiersDistributorsTemplate}" 
      SelectedValuePath="CurrentSupplier">    
</TreeView> 

對這個有什麼想法?
當我在方法「OnCollectionViewCurrentChanged」中設置斷點時,當我單擊一個樹視圖節點時什麼也沒有發生。即「CurrentSupplier」永遠不會更新,所以我不能在另一個方法中使用CurrentSupplier(爲另一個控件加載集合)。

感謝

回答

1
+0

明白了,放棄了應用,並使用這裏所描述的架構工作: http://msdn.microsoft.com/en -us/magazine/dd419663.aspx 然後,treeview所選項目(SupplierID,PartID等)將用作GridView控件的ItemSource集合的輸入。 看到這篇文章: http://tomlev2.wordpress.com/2009/04/17/wpf-binding-to-an-asynchronous-collection/ – 2010-07-26 18:37:42