I'm trying to create a extended treeview control inheriting from the existing winform TreeView control. Created a
Load()
function in the class TreeViewEx. In this function the dataSource is looped in a foreach. This foreach then calls the
Where()
extension method on the looping dataSource passing to it a methode (which takes as parameter the current element) returning a predicate. This predicate misintepretes the parameter value passed to it. It seems to be using previous parameter values.
value of arg in method before returning predicate =>
Image[
^]
value of arg when debugger enters predicate =>
Image[
^]
What I have tried:
Initially i thought this behavior was due to the fact that i am iterating through an Enumerable not a list, so i change the different enumerables to List but nothing changed. Also tried to instatiate the returned predicate but nothing.
Load function :
public Func<T, Func<T, bool>> GetChildrenPredicate { get; set; }
.
.
.
public virtual void Load(List<T> dataSource = null)
{
try
{
if (CreateNode == null)
{
OnError?.Invoke(this, new ArgumentNullException("CreateNode"));
return;
}
if (GetParentKey == null)
{
OnError?.Invoke(this, new ArgumentNullException("GetParentKey"));
return;
}
if (GetChildrenPredicate == null)
{
OnError?.Invoke(this, new ArgumentNullException("GetChildrenPredicate"));
return;
}
var finalDataSource = dataSource ?? DataSource;
TreeNode node = null;
BeginUpdate();
foreach (var item in finalDataSource)
{
node = CreateNode(item);
node.Tag = item;
if (this.Nodes.Find(node.Name, true).Count() == 0)
{
var n = this.Nodes.Find(this.GetParentKey(item), true).FirstOrDefault() as TreeNode;
if (n == null)
{
this.Nodes.Add(node);
}
else
{
n.Nodes.Add(node);
}
List<T> children = finalDataSource
.ToList()
.Where(this.GetChildrenPredicate(item))
.ToList();
if (children.Count() > 0)
{
Load(children);
}
}
}
EndUpdate();
}
catch (Exception ex)
{
OnError?.Invoke(this, ex);
}
}
GetChildrenPredicate :
private Func<ORM.DataModels.Menu, bool> GetChildrenPredicate(ORM.DataModels.Menu arg)
{
return (ORM.DataModels.Menu m) =>
(m.Lepere == arg.Codmen) ||
(m.Lepere == null && arg.Codmen == "_" + m.Niveau);
}