This is a migrated thread and some comments may be shown as answers.

[Solved] Using AlignmentContentPresenter - XamlParseException - Element is already the child of another element

2 Answers 145 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Rick Glos
Top achievements
Rank 1
Rick Glos asked on 10 Sep 2009, 10:05 PM
Hi there,

I'm attempting to refactor my original code to use the new AlignmentContentPresenter.

Here's a link to our old discussion.

As a quick recap, I'm dynamically creating the columns because users are defining 'views' dynamically - picking and choosing which columns are in the GridView.  Here's some link to our old discussions (dynamic columns & binding to an object built at run-time) I used to get it working.

Anyway...

I'm receiving an error with the message of "Element is already the child of another element."

I've been trying to troubleshoot this.  It's a strange one because if I just set the Header property it works fine.  The problem occurs only when I start using the AlignmentContentPresenter.  Then, when using the AlignmentContentPresenter, if I create the columns dynamically via the constructor it works fine but if I create the columns outside the constructor I get the error.

        public MainPage() 
        { 
            InitializeComponent(); 
 
            List<Person> people = new List<Person>(); 
            people.Add(new Person() { Name = "Joe Bob", Age = 37 }); 
            people.Add(new Person() { Name = "Johnny Lunchbox", Age = 25 }); 
            people.Add(new Person() { Name = "Jim Beam", Age = 25 }); 
            this.DataContext = people; 
 
            // If I call CreateColumns() here it works hmm... but via the click event on the button it does not 
            // CreateColumns() 
        } 
 
        private void CreateColumns() 
        { 
            // TODO: Simulate a long running process 
 
            //Dispatcher.BeginInvoke(() => { uxRadGridView.Columns.Add(CreateColumn("Name", "Name 1", "This is the Name 1 column.")); }); 
            for (int i = 1; i < 26; i++) 
            { 
                uxRadGridView.Columns.Add(CreateColumn("Name""Name " +  i, "This is the Name " + i + " column.")); 
                // This is where the XamlParseException will occur 
                uxRadGridView.Columns.Add(CreateColumn("Age""Age " + i, "This is the Age " + i + " column.")); 
            } 
        } 
 
        private GridViewColumn CreateColumn(string uniqueName, string headerText, string toolTipText) 
        { 
            GridViewDataColumn column = new GridViewDataColumn(); 
            column.UniqueName = uniqueName; 
 
            //// This work either way - via button or constructor 
            //column.Header = headerText; 
 
            // This only works if CreateColumns() is called in the constructor 
            AlignmentContentPresenter alignmentContentPresenter = new AlignmentContentPresenter(); 
            alignmentContentPresenter.TextWrapping = TextWrapping.Wrap; 
            alignmentContentPresenter.Content = headerText; 
            column.Header = alignmentContentPresenter; 
            ToolTipService.SetToolTip(alignmentContentPresenter, toolTipText); 
 
            return column; 
        } 
 
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
            CreateColumns(); 
        } 
 
        public class Person 
        { 
            public string Name { getset; } 
            public int Age { getset; } 
        } 

I've created a sample to demonstrate the error.  Launch it and click on 'Create Columns' and you'll get the error.  Uncomment the line in the constructor and it works.  It also works if I comment out the block around the AlignmentContentPresenter.

Is this a bug with my code?  Am I using the AlignmentContentPresenter incorrectly?  I am hoping to use it to simplify my old code and add tool tips to the column headers.

Thanks!



2 Answers, 1 is accepted

Sort by
0
Accepted
Pavel Pavlov
Telerik team
answered on 14 Sep 2009, 08:56 AM
Hi Rick,

This week we have fixed a problem related to this exception. When you place an UIElement within the header template, in specific scenarios it gave such exception.

I believe the problem is fixed now. Please give the latest internal build a try and let me know if you still have a trouble. ( You should have the binaries available for download in your Client.NET account).

In case you have any further problems, just let me know  and we will provide a fix as soon as possible.

Regards,
Pavel Pavlov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Rick Glos
Top achievements
Rank 1
answered on 14 Sep 2009, 02:34 PM
The latest internal build, RadControls_for_Silverlight_2009_2_0911_Dev, fixed the problem.

Thank you.
Tags
GridView
Asked by
Rick Glos
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Rick Glos
Top achievements
Rank 1
Share this question
or