This question is locked. New answers and comments are not allowed.
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.
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!
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 { get; set; } |
| public int Age { get; set; } |
| } |
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!