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

The runtime use of the custom one DataTemplate for all the ColumnGroup's headers in a RadGridView control

1 Answer 110 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 25 May 2012, 09:50 AM

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.

1 Answer, 1 is accepted

Sort by
0
Dmitry
Top achievements
Rank 1
answered on 28 May 2012, 05:23 AM
Resolved. Just enough to use newGroup.HeaderTemplate = templateColumnGroup instead of newColumnGroup.Header = templateColumnGroup.LoadContent(). (The call to Header property was originally forced by the trial to set DataContext to the UI-content of the column group, but that's is another story.)


P.S. Nevertheless, maybe, is there any sample or workaround to set the templated Header manually? )
Tags
GridView
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Dmitry
Top achievements
Rank 1
Share this question
or