2010-07-02 3 views
1

WCF 및 MVVM 패턴을 사용하여 트리 뷰 컨트롤을 채우는 중이고 선택한 항목을 뷰 모델의 다른 메서드에 대한 매개 변수로 전달해야합니다 (다른 컨트롤을 채우기 위해)).WPF MVVM TreeView 선택된 항목에 현재 선택 개체가 채워지지 않음

트리 뷰가 잘 채워지지만 선택한 값이 뷰 모델에 전달되지 않습니다. 예 : XAML에서

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"); 

: 이것에

<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"메서드에서 중단 점을 설정하면 treeview 노드를 클릭해도 아무 일도 일어나지 않습니다. 즉 "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/ –

관련 문제