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(为另一个控件加载集合)。
感谢
明白了,放弃了应用,并使用这里所描述的架构工作: 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