「項目の 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
}