0

我正在尝试添加一个进度条,当在树视图中单击扩展项目时将显示该进度条。此栏应在单击项目后立即显示,并在项目展开后使其不可见。我试图为这个事件添加一个处理程序,我在这里找到了 ,但它不起作用。

我这样使用它:

 treeItemsToAdd = new MyTreeViewItem();
 treeItemsToAdd.Expanding += treeItemsToAdd_Expanding;

我无法弄清楚为什么这不起作用...... treeItemsToAdd_Expanding 事件仅在项目展开后才会触发

4

1 回答 1

-1

我建议在默认实现之前覆盖 OnExpanded 虚拟方法并引发 Expanding 事件。

public class MyTreeViewItem : TreeViewItem
{
        public static readonly RoutedEvent CollapsingEvent =
        EventManager.RegisterRoutedEvent("Collapsing",
             RoutingStrategy.Bubble, typeof(RoutedEventHandler),
             typeof(MyTreeViewItem));

    public static readonly RoutedEvent ExpandingEvent =
             EventManager.RegisterRoutedEvent("Expanding",
             RoutingStrategy.Bubble, typeof(RoutedEventHandler),
             typeof(MyTreeViewItem));

    public event RoutedEventHandler Collapsing
    {
        add { AddHandler(CollapsingEvent, value); }
        remove { RemoveHandler(CollapsingEvent, value); }
    }

    public event RoutedEventHandler Expanding
    {
        add { AddHandler(ExpandingEvent, value); }
        remove { RemoveHandler(ExpandingEvent, value); }
    }

    protected override void OnExpanded(RoutedEventArgs e)
    {
        OnExpanding(new RoutedEventArgs(ExpandingEvent, this));
        base.OnExpanded(e);
    }

    protected override void OnCollapsed(RoutedEventArgs e)
    {
        OnCollapsing(new RoutedEventArgs(CollapsingEvent, this));
        base.OnCollapsed(e);
    }

    protected virtual void OnCollapsing(RoutedEventArgs e) { RaiseEvent(e); }
    protected virtual void OnExpanding(RoutedEventArgs e) { RaiseEvent(e); }
}
于 2013-03-26T15:17:43.753 回答