我正在尝试将来自相关数据表客户<-产品的信息表示为树形视图。
由于性能问题,我想使用 Backgroundworker,但有一个问题 - 没有 GUI 单元(也可能是 STA 之外的其他线程中的 TreeViewItem)。
所以我定义了我自己的“STA-thread-safe”类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace XXX.YYY
{
public class TreeViewNode: INotifyPropertyChanged
{
#region [ Header ]
private string _header;
public string Header
{
get
{
return _header;
}
set
{
_header = value;
OnNotifyPropertyChanged("Header");
}
}
#endregion
#region [ Tag ]
private string _tag;
public string Tag
{
get
{
return _tag;
}
set
{
_tag = value;
OnNotifyPropertyChanged("Tag");
}
}
#endregion
#region [ Items ]
private ObservableCollection<TreeViewNode> _items;
public ObservableCollection<TreeViewNode> Items
{
get
{
if (_items == null)
{
_items = new ObservableCollection<TreeViewNode>();
}
return _items;
}
set
{
_items = value;
OnNotifyPropertyChanged("Items");
}
}
#endregion
#region [ Property_Changed_Utilities ]
public event PropertyChangedEventHandler PropertyChanged;
private void OnNotifyPropertyChanged(String info)
{
// Note: Do not forget to add interface
// "INotifyPropertyChanged" to your class.
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
public TreeViewNode()
{
}
}
}
XAML 中的模板用于表示:
<TreeView Name="treeview"
ItemsSource="{Binding RelativeSource=
{RelativeSource Mode=FindAncestor,AncestorType=Window},
Path=TreeViewData}"
SelectedItemChanged="treeview_SelectedItemChanged">
<TreeView.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TreeViewItem Header="{Binding Header}"
ItemsSource="{Binding Items}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
<TreeView.ItemTemplate>
<DataTemplate>
<TreeViewItem Header="{Binding Header}"
ItemsSource="{Binding Items}" />
</DataTemplate>
</TreeView.ItemTemplate>
到目前为止它正在工作 - 根据需要显示带有子项的填充树。也展开/折叠作品...
但是 - 没有事件发生 - SelectedItemChanged 没有反应。
任何帮助表示赞赏!非常感谢您!