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

[Solved] Problems with setting DataGridTemplateColumn's CellContentTemplate dynamically

1 Answer 206 Views
Grid for XAML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Yuri
Top achievements
Rank 1
Yuri asked on 04 Sep 2014, 07:36 PM
Hello.

I have a RadDataGrid with a single DataGridTemplateColumn.
I am trying to dynamically generate the DataTemplate for the column's cells and assign it to the CellContentTemplate in runtime.
DataTemplate is created by assembling its textual representation using a StringBuilder, and then calling 
                 dataTemplate = (DataTemplate)XamlReader.Load(stringBuilder.ToString());

The problem is that when the template contains anything beyond some rudimentary elements, I get a XAML exception.

I tried to implement this DataTemplate assignment in 2 different ways:
1. When the page is loaded, generate the DataTemplate and just assign (dataGrid.Columns[0] as DataGridTemplateColumn).CellContentTemplate = dataTemplate.
2. Implement a DataTemplateSelector whose SelectTemplateCore method returns the dynamically resolved DataTemplate.
The both approaches result in the same exception.

The following DataTemplate definition works:
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
                stringBuilder.Append("<Grid>");
                stringBuilder.Append("<Grid Margin=\"30,12,10,12\">");
                stringBuilder.Append("<StackPanel Orientation=\"Horizontal\">");
                stringBuilder.Append("</StackPanel>");
                stringBuilder.Append("</Grid>");
                stringBuilder.Append("</Grid>");
                stringBuilder.Append("</DataTemplate>");

The following, though, results in an exception:
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
                stringBuilder.Append("<Grid>");
                stringBuilder.Append("<Grid Margin=\"30,12,10,12\">");
                stringBuilder.Append("<StackPanel Visibility=\"{Binding Converter={StaticResource EmptyPatientListToVisibilityConverter}, ConverterParameter=True}\">");
                stringBuilder.Append("</StackPanel>");
                stringBuilder.Append("</Grid>");
                stringBuilder.Append("</Grid>");
                stringBuilder.Append("</DataTemplate>");

The converter is declared in the XAML file of the same code-behind:
         <converters:EmptyPatientListToVisibilityConverter x:Key="EmptyPatientListToVisibilityConverter"/>

The exception is:
"The text associated with this error code could not be found.\r\n\r\nElement is already the child of another element. [Line: 1 Position: 126]"
with the stack trace:
   at Windows.UI.Xaml.DataTemplate.LoadContent()
   at Telerik.UI.Xaml.Controls.Grid.DataGridTemplateColumn.CreateContainer(Object rowItem)
   at Telerik.UI.Xaml.Controls.Grid.View.XamlContentLayer.GenerateCellContainer(Object rowItem, DataGridColumn definition)
   at Telerik.UI.Xaml.Controls.Grid.XamlGridCellGenerator.Telerik.UI.Xaml.Controls.Grid.IUIContainerGenerator<Telerik.UI.Xaml.Controls.Grid.GridCellModel,Telerik.UI.Xaml.Controls.Grid.CellGenerationContext>.GenerateContainerForItem(CellGenerationContext info, Object containerType)
   at Telerik.UI.Xaml.Controls.Grid.CellModelGenerator.GenerateContainerForItem(CellGenerationContext context, Object containerType)
   at Telerik.UI.Xaml.Controls.Grid.ItemModelGenerator`2.GenerateContainer(K context)
   at Telerik.UI.Xaml.Controls.Grid.CellsController`1.GetCellDecorator(IItemInfoNode parentRow, ItemInfo columnItemInfo, Int32 rowLine, Int32 columnLine)
   at Telerik.UI.Xaml.Controls.Grid.CellsController`1.GenerateCellsForRow(IItemInfoNode rowDecorator, Int32 rowSlot)
   at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.GenerateCellsForReadOnlyRow(Int32 rowSlot, Double largestRowElementWidth, IItemInfoNode rowDecorator)
   at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.Telerik.UI.Xaml.Controls.Grid.ITable.GenerateCellsForRow(Int32 rowSlot, Double largestRowElementHeight, IItemInfoNode rowDecorator)
   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.GenerateCellsForLine(Int32 slot, Double largestLength, T lastElement)
   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.GenerateContainer(IList`1 itemInfos)
   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.MeasureForward(MeasureContext& context)
   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.MeasureVertically(RadSize availableSize, Double offset)
   at Telerik.UI.Xaml.Controls.Grid.NodePool`2.OnMeasure(RadSize availableSize, Double offset)
   at Telerik.UI.Xaml.Controls.Grid.Model.GridModel.MeasureCells(RadSize newAvailableSize)
   at Telerik.UI.Xaml.Controls.Grid.RadDataGrid.OnCellsPanelMeasure(RadSize newAvailableSize)
   at Telerik.UI.Xaml.Controls.Grid.Primitives.DataGridCellsPanel.MeasureOverride(Size availableSize)
   at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
   at Telerik.UI.Xaml.Controls.Grid.Primitives.DataGridRootPanel.MeasureOverride(Size availableSize)
   at Windows.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
   at Telerik.UI.Xaml.Controls.RadControl.MeasureOverride(Size availableSize)


Please advise.




1 Answer, 1 is accepted

Sort by
0
Yuri
Top achievements
Rank 1
answered on 04 Sep 2014, 07:46 PM
Hmmmm...
Turns out that moving the converter declaration from the page's XAML file to StandardStyles.xaml solves the issue.
Not very intuitive, and goes against the encapsulation idea...
Tags
Grid for XAML
Asked by
Yuri
Top achievements
Rank 1
Answers by
Yuri
Top achievements
Rank 1
Share this question
or