tags:

views:

14

answers:

0

Cannot add logical and visual child to Adorner class.

Blockquote

I have a WPF application.Infragistics grid and xamtext boxes are using. I implement the concept of history.ie when user is click on the cell or textbox, a suggestion box is being shown which contains the previous values.Sample screen shot is attaching.

Blockquote

![alt text][1]

I implemented the suggestion box using the concept of adorners.A WPF usercontrol is created which contains a listbox and then it is added to the adornerClass . Issue: I tried to avoid as much memory leak. Present implementation:-No crash but memory leak. The present implementation is working fine.But each time the user click in the grid cell, a user control is being created and passed to adorner class.Since the usercontrol xaml using a lot of resources like styles the memory used for it is not releasing.so I tried to create the user control instance only once. Crashing when addLogicalChild to adorner. I tried to make the usercontrol instance as a singleton object. when a user click on a cell, the usercontrol UI class’s object will create iff it is null, else the existing instance will be returned. But the adorner will allow the creation

Blockquote Sample code portion

using System; using System.Collections; using System.Windows; using System.Windows.Documents; using System.Windows.Media;

namespace BenderSim.Data.History { /// /// An adorner that can display one and only one UIElement.
/// That element can be a panel, which contains multiple other elements. /// The element is added to the adorner's visual and logical trees, enabling it to /// particpate in dependency property value inheritance, amongst other things. /// public class UIElementAdorner : Adorner where TElement : UIElement { #region Data

    TElement _child = null;
    double _offsetLeft = 0;
    double _offsetTop = 0;

    #endregion // Data

    #region Constructor

    /// <summary>
    /// Constructor.  Adds 'childElement' to the adorner's visual and logical trees.
    /// </summary>
    /// <param name="adornedElement">The element to which the adorner will be bound.</param>
    /// <param name="childElement">The element to be displayed in the adorner.</param>
    public UIElementAdorner(UIElement adornedElement, TElement childElement)
        : base(adornedElement)
    {
        if (childElement == null)
            throw new ArgumentNullException("childElement");

        _child = childElement;


        this.AddLogicalChild(childElement);           //Here fails.During second attempt
        this.AddVisualChild(childElement);
    }

Blockquote

/*This call will work fine because each time a new instance is passing. */ _adorner = new UIElementAdorner(sourceGrid, new UserCntrl_SuggestionListBox(sourceGrid,ColumnId,GetControlValueHistory(ColumnId))); layer.Add(_adorner);

/*This call fails since try to pass the existing instance */ adorner = new UIElementAdorner(sourceGrid, BenderSim.UI.UIControlHandler.Instance.GetHistorySuggestionBox());

The following exception came when we use the same usercontrol object each time. ![alt text][2]

But if I use different usercontrol instances each time, then the memory leak issue exist.