tags:

views:

254

answers:

1

I'm using WCF and MVVM pattern to populate a tree view control and I need the selected item passed as a parameter for another method in the view model (to populate a different control).

The treeview populates just fine but the selected value is not being passed to the view model. e.g. in the viewmodel:

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

in the 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>

Any thoughts on this?
When I set a breakpoint in the method "OnCollectionViewCurrentChanged", nothing ever happens when I click on a treeview node. i.e. "CurrentSupplier" never gets updated, so I can't use CurrentSupplier in another method I have (to load a collection for another control).

Thanks

+1  A: 

This solution works for me:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/cc73893a-3383-4328-a002-ed8fb002a19d

codekaizen
Got it to work by scrapping the app and using the architecture described here:http://msdn.microsoft.com/en-us/magazine/dd419663.aspxThe treeview's selected item (SupplierID, PartID, etc) is then used as an input for a gridview control's ItemSource collection. see this article:http://tomlev2.wordpress.com/2009/04/17/wpf-binding-to-an-asynchronous-collection/
Brian Wells