private void AddOperand() { if (SelectedFilterConfiguration == null || SelectedFilterableArgument == null) { return; } var fieldLeaf = new ExpressionTreeFieldLeaf(null, SelectedFilterableArgument.BoundProperty); object value; if (SelectedFilterableArgument.DefaultValue != null) { value = SelectedFilterableArgument.DefaultValue; } else if (fieldLeaf.Property.PropertyType == typeof(DateTime)) { value = DateTime.Now; } else if (fieldLeaf.Property.PropertyType == typeof(String)) { value = String.Empty; } else if (fieldLeaf.Property.PropertyType.IsAbstract) { value = null; } else { value = Activator.CreateInstance(fieldLeaf.Property.PropertyType); } var valueLeaf = new ExpressionTreeValueLeaf(null, value); var compariosonNode = new ComparisonOperatorNode(null, fieldLeaf, valueLeaf, SelectedFilterableArgument.AvailableOperators.FirstOrDefault()); //First node always add as comparision node if (SelectedFilterConfiguration.Conditions.Root == null) { SelectedFilterConfiguration.Conditions.Root = compariosonNode; } else { //Default add join node as AND operator if (SelectedExpression == null) { //If adding to root var rootBooleanNode = new BooleanOperatorNode(null, compariosonNode, SelectedFilterConfiguration.Conditions.Root, BooleanOperators.And); SelectedFilterConfiguration.Conditions.Root = rootBooleanNode; } else if (SelectedExpression is BooleanOperatorNode && ((BooleanOperatorNode)SelectedExpression).Operator != BooleanOperators.Not && SelectedExpression.Left == null) { //Adding to existing node SelectedExpression.Left = compariosonNode; SelectedFilterConfiguration.Conditions.Root = SelectedFilterConfiguration.Conditions.Root.Clone() as ExpressionTreeNode; } else if (SelectedExpression.Parent is BooleanOperatorNode && ((BooleanOperatorNode)SelectedExpression.Parent).Operator != BooleanOperators.Not && SelectedExpression.Parent.Left == null) { //Adding to existing node SelectedExpression.Parent.Left = compariosonNode; SelectedFilterConfiguration.Conditions.Root = SelectedFilterConfiguration.Conditions.Root.Clone() as ExpressionTreeNode; } else { //Adding to arbitrary node bool isParentNull = SelectedExpression.Parent == null; bool isSelectedBoolNode = SelectedExpression is BooleanOperatorNode; ExpressionTreeElement leftExpession; ExpressionTreeElement rightExpression; if (isSelectedBoolNode) { leftExpession = SelectedExpression.Right; } else if (isParentNull) { leftExpession = SelectedExpression; } else { leftExpession = SelectedExpression.Parent.Right; } if (leftExpession is BooleanOperatorNode) { rightExpression = leftExpession; leftExpession = compariosonNode; } else { rightExpression = compariosonNode; } var newBoolNode = new BooleanOperatorNode( SelectedExpression.Parent, leftExpession != null ? (leftExpession.Clone() as ExpressionTreeElement) : null, rightExpression.Clone() as ExpressionTreeElement, BooleanOperators.And); if (isParentNull && !isSelectedBoolNode) { SelectedFilterConfiguration.Conditions.Root = newBoolNode; } else { Trace.WriteLine("Before adding to SelectedExpression.Parent\r\n" + SelectedExpression.Parent); if (isSelectedBoolNode) { SelectedExpression.Right = newBoolNode; } else { SelectedExpression.Parent.Right = newBoolNode; } Trace.WriteLine("After adding to SelectedExpression.Parent\r\n" + SelectedExpression.Parent); SelectedFilterConfiguration.Conditions.Root = SelectedFilterConfiguration.Conditions.Root.Clone() as ExpressionTreeNode; } Trace.WriteLine("Final expression after adding\r\n" + SelectedFilterConfiguration.Conditions.Root); } } if (SelectedFilterConfiguration.Conditions.Root != null) { Task.Run(() => { Thread.Sleep(100); Application.Current.Dispatcher.Invoke(() => SelectedExpression = SelectedFilterConfiguration.Conditions.Root.Find(compariosonNode) as ExpressionTreeNode); }); } else { SelectedExpression = null; } }