「遞歸調用函數時的項目CheckState變化(並確保後續ItemCheck事件,而編程改變CheckStates被忽略)」
如果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個簡單+ 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
}
非常感謝,我才發現這個問題我自己,但是我改了行的問題1如果'(br.CanExpand)br.FetchChildren();'。你有沒有注意到這種修改的副作用?它確實看起來是正確的 - 但這不會修改引起所有問題的分支的'Expanded'屬性:) +1無論是否查看源代碼(我應該早一點做!) – Simon 2013-02-15 14:34:05
代碼更改據我所知,似乎沒有引起任何副作用。與ObjectListView(我之前使用過很多次,沒有任何重大問題)相比,TreeListView相當麻煩。我花了一些時間尋找一個或多或少的同等(免費)替代品,但似乎沒有任何東西在那裏。即使遇到麻煩,它仍然是一個很好的組成部分。快樂編碼... – 2013-02-15 14:45:13
謝謝Rev1.0 - 我同意迄今爲止最好的,但有點怪癖。 – Simon 2013-02-15 14:46:43