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