Good day time! There is some issue about the RadGridView control feature concerned merged columns' headers. We need to create custom ColumnGroup.Header's content from some DataTemplate at runtime. Here is the simplified sample:
MainPageViewModel.cs:
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace GtaRadGridView.GridView.ViewModel
{
public class DataItem
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
public class MainPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<DataItem> DataItems { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public MainPageViewModel()
{
DataItems = new ObservableCollection<DataItem>();
DataItems.Add(new DataItem { PropertyA = "ValueA", PropertyB = "ValueB", PropertyC = "ValueC" });
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs("DataItems"));
}
}
}
}
MainPage.xaml:
<UserControl x:Class="GtaRadGridView.GridView.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:vm="clr-namespace:GtaRadGridView.GridView.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<DataTemplate x:Key="templateColumnGroup" DataType="telerik:GridViewColumnGroup">
<TextBlock HorizontalAlignment="Center" Text="Group1"/>
</DataTemplate>
</UserControl.Resources>
<UserControl.DataContext>
<vm:MainPageViewModel />
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="White">
<telerik:RadGridView x:Name="gridviewMain" ItemsSource="{Binding DataItems}" />
</Grid>
</UserControl>
MainPage.xaml.cs:
using System.Windows;
using Telerik.Windows.Controls;
using GtaRadGridView.GridView.ViewModel;
namespace GtaRadGridView.GridView
{
public partial class MainPage
{
public MainPage()
{
InitializeComponent();
gridviewMain.ColumnGroups.CollectionChanged +=
(sender, args) =>
{
if (null == args.NewItems) return;
var templateColumnGroup = Resources["templateColumnGroup"] as DataTemplate;
if (null == templateColumnGroup) return;
foreach (var newItem in args.NewItems)
{
var newColumnGroup = newItem as GridViewColumnGroup;
if (null == newColumnGroup) continue;
// Causes problem
newColumnGroup.Header = templateColumnGroup.LoadContent();
// Works properly
newColumnGroup.Header = "Group";
}
};
gridviewMain.Columns.CollectionChanged +=
(sender, args) =>
{
if (null == args.NewItems) return;
foreach (var newItem in args.NewItems)
{
var newColumn = newItem as GridViewColumn;
if (null == newColumn) continue;
newColumn.ColumnGroupName = "First";
}
};
Loaded +=
(sender, args) =>
{
var vmMainPage = DataContext as MainPageViewModel;
if (null == vmMainPage) return;
gridviewMain.ColumnGroups.Add(new GridViewColumnGroup { Name = "First" });
};
}
}
}
Unfortunately, however the application is launched successfully and looks properly, the unhandled exception is thrown immediately every time: "The element is a child of the other element already" via stack trace:
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
at MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty property, Object obj)
at System.Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value)
at System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue)
at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isBindingInStyleSetter)
at System.Windows.Controls.ContentControl.set_Content(Object value)
at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.ArrangeLowestHeaders()
at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.ArrangeHeaders()
at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.PlaceCommonHeaders()
at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.Rearrangeheaders()
at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.NotifyCellsPropertyChanged(DependencyObject d, String propertyName, DependencyPropertyChangedEventArgs e, UpdateTarget target)
at Telerik.Windows.Controls.GridView.GridViewHeaderRow.NotifyPropertyChanged(DependencyObject d, String propertyName, DependencyPropertyChangedEventArgs e, UpdateTarget target)
at Telerik.Windows.Controls.GridView.GridViewDataControl.NotifyPropertyChanged(DependencyObject d, String propertyName, DependencyPropertyChangedEventArgs e, UpdateTarget target)
at Telerik.Windows.Controls.GridViewColumnCollectionInternal.CalculateColumnWidths(Object arg)
The problem's root is suspected in the use of ColumnGroup.Header = DataTemplate.LoadContent() construction at runtime because the one being commented leads to elimination of the exception. Maybe, someone could help us to solve this? :)
P.S. The Telerik version for the SL5 is the Q1.326 of February.