ObjectListView - TreeListView Auto TriStateChecking

时间:2013-02-15 12:40:08

标签: c# recursion objectlistview treelistview

是否有人在TreeListView中成功获得三态复选框,而不使用模型对象中的属性来存储checkstate?

因此,例如,检查节点。应检查其所有儿童(和儿童等),然后根据其兄弟姐妹CheckStates检查其所有父母/祖父母。

我尝试了几种方法,但每种方法都在TreeListView类中得到ArgumentOutOfRange / ArgumentException。这包括以下内容:

  • 在词典中存储所有节点CheckStates并用作CheckStateGetter事件的查找
  • 当项目CheckState更改时递归调用函数(并确保在编程更改CheckStates时忽略后续的ItemCheck事件)
  • 调用函数以确定直接子/父状态,并让TreeListView为每个受影响的节点触发ItemChecked事件。

我经常从以下函数中获取错误(在TreeListView.cs中):

  • GetNthItem()
  • GetChildren() - 然后在GUI中扩展/折叠shild
  • ProcessLButtonDown()

如果有人在这里取得了成功,我全都听见了。

2 个答案:

答案 0 :(得分:1)

我也遇到了TreeListView的一些问题,并在GetChildren()函数中发现了一个问题。

GetChildren不必要地尝试扩展相关分支以获取子级。这导致内部状态在视图保持折叠状态时显示为已展开,并导致内部索引出现问题。

原始方法:

    public virtual IEnumerable GetChildren(Object model) {
        Branch br = this.TreeModel.GetBranch(model);
        if (br == null || !br.CanExpand)
            return new ArrayList();

        if (!br.IsExpanded)  // THIS IS THE PROBLEM
            br.Expand();

        return br.Children;
    }

固定方法:

    public virtual IEnumerable GetChildren(Object model) {
        Branch br = this.TreeModel.GetBranch(model);
        if (br == null || !br.CanExpand)
            return new ArrayList();

        br.FetchChildren();

        return br.Children;
    }

也许这至少可以解决你的一些问题。

答案 1 :(得分:0)

  

“当项目CheckState发生更改时递归调用函数(并确保在编程更改CheckStates时忽略后续的ItemCheck事件)”

如果TreeListView自检(“来自框”)对您有好处,您只想知道手册后所有工作人员点击完成时的主要复选框 (这意味着:你点击,TreeListView(联合国)检查所有的孩子和父母), 所以你需要在ObjectListView库中制作额外的事件(粗略地):

\ objectlistviewdemo \ objectlistview \ treelistview.cs

首先添加事件(我的事件很糟糕,但你知道要修复的内容)

    #region Events

    public delegate void AfterTreeCheckEventHandler(object sender/*, object rowmodel*/);

    /// <summary>
    /// Triggered when clicked main checkbox checked and all related too.
    /// </summary>
    [Category("ObjectListView"),
    Description("This event is triggered when clicked main checkbox checked and all related too.")]
    public event AfterTreeCheckEventHandler AfterTreeCheckAndRecalculate;

    #endregion

    #region OnEvents

    /// <summary>
    /// Tell the world when a cell has finished being edited.
    /// </summary>
    protected virtual void OnAfterTreeCheckAndRecalculate(/*CellEditEventArgs e*/)
    {
        if (this.AfterTreeCheckAndRecalculate != null)
            this.AfterTreeCheckAndRecalculate(this/*, e*/);
    }

    #endregion

其次你需要修复“SetObjectCheckedness”方法(1个简单的递归方法,1个递归)

        /// <summary>
    /// Change the check state of the given object to be the given state.
    /// </summary>
    /// <remarks>
    /// If the given model object isn't in the list, we still try to remember
    /// its state, in case it is referenced in the future.</remarks>
    /// <param name="modelObject"></param>
    /// <param name="state"></param>
    /// <returns>True if the checkedness of the model changed</returns>
    protected override bool SetObjectCheckedness(object modelObject, CheckState state) {
        // If the checkedness of the given model changes AND this tree has 
        // hierarchical checkboxes, then we need to update the checkedness of 
        // its children, and recalculate the checkedness of the parent (recursively)

        bool result = SetObjectCheckednessHelper(modelObject, state, 0);

        if (this.AfterTreeCheckAndRecalculate != null)
            this.AfterTreeCheckAndRecalculate(this); //report that work is done
        return result;
    }

    protected bool SetObjectCheckednessHelper(object modelObject, CheckState state, int i) //recursive
    {
        if (!base.SetObjectCheckedness(modelObject, state))
            return false;

        if (!this.HierarchicalCheckboxes)
            return true;

        // Give each child the same checkedness as the model

        CheckState? checkedness = this.GetCheckState(modelObject);
        if (!checkedness.HasValue || checkedness.Value == CheckState.Indeterminate)
            return true;

        foreach (object child in this.GetChildrenWithoutExpanding(modelObject))
        {
            this.SetObjectCheckednessHelper(child, checkedness.Value, i+1);
        } //(un)check all children checkboxes

        if (i == 0) //recalculate upper levels only in the case of first call
        {
            ArrayList args = new ArrayList();
            args.Add(modelObject);
            this.RecalculateHierarchicalCheckBoxGraph(args); //all upper checkboxes in intermediate state or (un)check 
        }   

        return true;
    }

用法:

    this.olvDataTree.AfterTreeCheckAndRecalculate += new BrightIdeasSoftware.TreeListView.AfterTreeCheckEventHandler(this.olvDataTree_TreeChecked); 

    private void olvDataTree_TreeChecked(object sender)
    {
            //some staff here
    }