【发布时间】:2010-06-01 15:24:49
【问题描述】:
我正在学习如何在 WPF 中为 TreeView 使用数据绑定。我在程序上创建Binding 对象,设置Source、Path 和Converter 属性指向我自己的类。我什至可以设置IsAsync,当我探索树时,我可以看到 GUI 异步更新。到目前为止一切顺利!
我的问题是 WPF 在 GUI 中扩展树的某些部分之前急切地评估它们。如果留得足够长,这将导致整个树被评估(实际上在这个例子中我的树是无限的,但你明白了)。 我希望仅在用户扩展节点时按需评估树。这是否可以使用 WPF 中现有的异步数据绑定东西?
顺便说一句,我还没有弄清楚 ObjectDataProvider 与这项任务的关系。
我的 XAML 代码只包含一个 TreeView 对象,而我的 C# 代码是:
public partial class Window1 : Window
{
public Window1() {
InitializeComponent();
treeView.Items.Add( CreateItem(2) );
}
static TreeViewItem CreateItem(int number)
{
TreeViewItem item = new TreeViewItem();
item.Header = number;
Binding b = new Binding();
b.Converter = new MyConverter();
b.Source = new MyDataProvider(number);
b.Path = new PropertyPath("Value");
b.IsAsync = true;
item.SetBinding(TreeView.ItemsSourceProperty, b);
return item;
}
class MyDataProvider
{
readonly int m_value;
public MyDataProvider(int value) {
m_value = value;
}
public int[] Value {
get {
// Sleep to mimick a costly operation that should not hang the UI
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Debug.Write(string.Format("Evaluated for {0}\n", m_value));
return new int[] {
m_value * 2,
m_value + 1,
};
}
}
}
class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
// Convert the double to an int.
int[] values = (int[])value;
IList<TreeViewItem> result = new List<TreeViewItem>();
foreach (int i in values) {
result.Add(CreateItem(i));
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new InvalidOperationException("Not implemented.");
}
}
}
注意:我之前设法通过添加 WPF 事件处理程序并在触发事件处理程序时直接添加项目来对树节点进行延迟评估。我正试图摆脱这种情况并改用数据绑定(我理解这更符合“WPF 方式”的精神)。
【问题讨论】:
-
你能描绘出你的树形结构吗?多少级?项目和子项目是否属于同一类型?
标签: wpf data-binding asynchronous lazy-evaluation