Rad Pane –ItemTemplate- Re-Initialized Issue while switching Tab.

1 posts, 0 answers
  1. Murugaperumal
    Murugaperumal avatar
    1 posts
    Member since:
    Feb 2012

    Posted 16 Feb 2012 Link to this post

    Hi Team,

    I am currently using Telerik Q2 2011. I am using ItemTemplate in RadPaneGroup. My requirement is whenever I click ‘New’ button new instance of ‘View’ will create and added into RadPaneGroup. up to now everything is working fine. Now when I am switching between RadPane, the ‘View’ is getting re-initialized and loaded instead of existing instance. So any event call or Service call in 'View' constructor will fire again while switching between RadPanes. My requirement is to have different instances of VIews in each RadPanes. So I can't use IsContentPreserved property of the PaneGroup because it won’t create new instace of ‘View’ when I click ‘New’ button. Could you please tell me how to stop re-initialization of ‘View’ when switching RadPane.

    I have pasted my code below.

     

    MainPage.xaml 
      
    <UserControl.Resources>
              
            <DataTemplate x:Key="MyContent">
                <views:CustomUserControl />
            </DataTemplate>
             
        </UserControl.Resources>
         
        <Grid x:Name="LayoutRoot" Background="White">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="3*"/>
            </Grid.ColumnDefinitions>
            <StackPanel Orientation="Vertical">
                <Button Content="Add New Rad Pane" x:Name="btnAdd" Click="btnAdd_Click" ></Button>
                 
            </StackPanel>
            <telerikDocking:RadDocking Grid.Column="1" >
                <telerikDocking:RadDocking.DocumentHost>
                    <telerikDocking:RadSplitContainer> <!-- IsContentPreserved="True"--> 
                        <telerikDocking:RadPaneGroup Name="radPaneGroup"  ItemTemplate="{StaticResource MyContent}" />
                    </telerikDocking:RadSplitContainer>
                </telerikDocking:RadDocking.DocumentHost>
            </telerikDocking:RadDocking>       
        </Grid>
    MainPage.xaml.cs
      
    public partial class MainPage : UserControl
        {   
            DataTemplate myContent;
            int header = 1;
      
            public MainPage()
            {
                InitializeComponent();
               
                myContent = this.Resources["MyContent"] as DataTemplate;
                radPaneGroup.AddItem(new TelerikControls.RadPane { Header = "Added Job " + header, ContentTemplate = myContent }, TelerikControls.Docking.DockPosition.Center);
            }
      
            private void btnAdd_Click(object sender, RoutedEventArgs e)
            {
                header++;
                radPaneGroup.AddItem(new TelerikControls.RadPane { Header = "Added Job " + header, ContentTemplate = myContent },TelerikControls.Docking.DockPosition.Center);           
            }       
      
        }
    CustomUserControl.xaml
      
    <UserControl x:Class="DockingDemo.Views.CustomUserControl"
        mc:Ignorable="d"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"             
        d:DesignHeight="768" d:DesignWidth="1040">  
         
       <Grid x:Name="LayoutRoot" HorizontalAlignment="Stretch" Height="650" Width="1040" VerticalAlignment="Stretch" Background="White">                   
            <TextBlock Name="text"  Text="custom user control  " Grid.Row="14" Grid.Column="4" ></TextBlock>        
        </Grid>
         
    </UserControl>
    CustomUserControl.xaml.cs
      
     public partial class CustomUserControl : UserControl
        {
            static int count = 1;
            public CustomUserControl()
            {
                InitializeComponent();
                text.Text = text.Text + count++;
            }
        }
    PaneGroupExtensions
      
    public class PaneGroupExtensions
        {
            private RadPaneGroup Group { get; set; }
      
            private static PaneGroupExtensions GetPaneGroupExtension(DependencyObject obj)
            {
                return (PaneGroupExtensions)obj.GetValue(PaneGroupExtensionProperty);
            }
      
            private static void SetPaneGroupExtension(DependencyObject obj, PaneGroupExtensions value)
            {
                obj.SetValue(PaneGroupExtensionProperty, value);
            }
      
            private static readonly DependencyProperty PaneGroupExtensionProperty =
                DependencyProperty.RegisterAttached("PaneGroupExtension", typeof(PaneGroupExtensions), typeof(PaneGroupExtensions), null);
      
            public static IEnumerable<RadPane> GetItemsSource(DependencyObject obj)
            {
                return (IEnumerable<RadPane>)obj.GetValue(ItemsSourceProperty);
            }
      
            public static void SetItemsSource(DependencyObject obj, IEnumerable<RadPane> value)
            {
                obj.SetValue(ItemsSourceProperty, value);
            }
      
            public static readonly DependencyProperty ItemsSourceProperty =
                DependencyProperty.RegisterAttached("ItemsSource", typeof(IEnumerable<RadPane>), typeof(PaneGroupExtensions), new PropertyMetadata(null, OnItemsSourceChanged));
      
            private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var group = d as RadPaneGroup;
                var oldValue = e.OldValue as IEnumerable<RadPane>;
                var newValue = e.NewValue as IEnumerable<RadPane>;
                var oldValueObservableCollection = e.OldValue as INotifyCollectionChanged;
                var newValueObservableCollection = e.NewValue as INotifyCollectionChanged;
      
                if (group != null)
                {
                    var extension = GetPaneGroupExtension(group);
                    if (extension == null)
                    {
                        extension = new PaneGroupExtensions { Group = group };
                        SetPaneGroupExtension(group, extension);
                    }
      
                    if (oldValue != null)
                    {
                        foreach (var pane in oldValue)
                        {
                            pane.RemoveFromParent();
                        }
      
                        if (oldValueObservableCollection != null)
                        {
                            oldValueObservableCollection.CollectionChanged -= extension.OnItemsSourceCollectionChanged;
                        }
                    }
      
                    if (newValue != null)
                    {
                        foreach (var pane in newValue)
                        {
                            group.Items.Add(pane);
                        }
      
                        if (newValueObservableCollection != null)
                        {
                            newValueObservableCollection.CollectionChanged += extension.OnItemsSourceCollectionChanged;
                        }
                    }
                }
            }
      
            private void OnItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                // TODO: Make better implementation of this.
                if (e.Action == NotifyCollectionChangedAction.Reset)
                {
                    this.Group.Items.Clear();
                    foreach (var pane in GetItemsSource(this.Group))
                    {
                        this.Group.Items.Add(pane);
                    }
                }
                else
                {
                    if (e.OldItems != null)
                    {
                        foreach (var pane in e.OldItems.OfType<RadPane>())
                        {
                            pane.RemoveFromParent();
                        }
                    }
      
                    if (e.NewItems != null)
                    {
                        int i = e.NewStartingIndex;
                        foreach (var pane in e.NewItems.OfType<RadPane>())
                        {
                            this.Group.Items.Insert(i++, pane);
                        }
                    }
                }
            }
        }


    Thanks in advance.
    Muruga

     

Back to Top