Add event to datatemplate in code-behind

3 posts, 1 answers
  1. Florian Perrichot
    Florian  Perrichot avatar
    4 posts
    Member since:
    Oct 2009

    Posted 13 Nov 2009 Link to this post


    I've created a Treelistview with some dynamic columns. Each column contains a datatemplate created in code behind (I need to define a binding for each column). My datatemplate contains a customcontrol and I want to subscribe to an event defined into the custom control.

    So, I haven't found any event in Page or TreelistView which will be launch after the tree view creation (template are used...). Have-you a solution or another workaround ? My current workaround is try to use the LayoutUpdated event.

            void MainPage_Loaded(object sender, RoutedEventArgs e) 
                ProjectServiceClient client = new ProjectServiceClient(); 
                client.GetProjectCompleted += new EventHandler<GetProjectCompletedEventArgs>(client_GetProjectCompleted); 
                client.GetProjectAsync(new GetProjectRequest()); 
                DateTime date = DateTime.Now.AddDays(-10); 
                for (int i = 0; i < dateNumber; i++) 
                    date = date.AddDays(1); 
                    CreateColumn(i, date); 
            public DataTemplate CreateDataTemplate(int index) 
                string xamlstring = @"
                        <local:WorklogControl Text='{Binding Path=Works[" + index + @"].ElapsedTime, Mode=OneWay}' Value='{Binding Path=Works[" + index + @"], Mode=OneWay}' />
                DataTemplate dt = XamlReader.Load(xamlstring) as DataTemplate; 
                return dt; 
            public void AddEvents() 
                List<WorklogControl> worklogs = XamlUtilities.GetChildren<WorklogControl>(workLogTree); 
                foreach (WorklogControl control in worklogs) 
                    control.TextClicked -= worklog_TextClicked; 
                    control.TextClicked += worklog_TextClicked; 
                    control.ButtonClicked -= worklog_ButtonClicked; 
                    control.ButtonClicked += worklog_ButtonClicked; 
            private void CreateColumn(int index, DateTime date) 
                RadColumn column = new RadColumn(); 
                column.Header = String.Format("{0:dd/MM}", date); 
                column.Tag = date; 
                column.CellTemplate = CreateDataTemplate(index); 
                column.Visibility = Visibility.Visible; 
                column.PropertyName = String.Format("Works[{0}]", index); 
                column.DisplayMemberPath = String.Format("Works[{0}]", index); 
                workLogTree.LayoutUpdated +=new EventHandler(workLogTree_LayoutUpdated); 
            void workLogTree_LayoutUpdated(object sender, EventArgs e) 
            List<WorkElement> BuildWorks(Project project) 
                List<WorkElement> works = new List<WorkElement>(); 
                foreach (DateTime date in dateList) 
                    works.Add(new WorkElement { Date = date, ElapsedTime = project.Id + date.DayOfYear }); 
                return works; 
            void client_GetProjectCompleted(object sender, GetProjectCompletedEventArgs e) 
                ObservableCollection<Project> results = e.Result.GetProjectResult; 
                ObservableCollection<Worklog> worksCollection = new ObservableCollection<Worklog>(); 
                foreach (Project project in results) 
                workLogTree.ItemsSource = worksCollection; 
            private void worklog_TextClicked(object sender, ClickedEventArgs e) 
                Comment.Text = ""
                timeSlider.Value = 60; 
            private void worklog_ButtonClicked(object sender, ClickedEventArgs e) 
                RadWindow.Confirm(new DialogParameters() 
                    Header = "Delete a worklog"
                    Content = "Are you sure you want to delete this worklog ?" 

    Another question,do you have plan to add a new feature like fix the first column (with the treeview) ? It can help when the Treeview contains a horizontal scrollbar and many columns.



  2. Answer
    Miroslav avatar
    922 posts

    Posted 18 Nov 2009 Link to this post

    Hi Florian Perrichot,

    Normally you cannot register for events in DataTemplates.

    I reckon that listening for the LayoutUpdated event is not the best way to go here.

    There are two ways to work around this:
    1. If you have the source for the control you can use the Telerik routed events. A routed event can travel up and down the visual tree and it can be handled at any point along the way. If you make your event a routed event, you can add just one handler at the TeeeList control to care for all elements.

    A routed event can be created like so:

    public static readonly RoutedEvent MyEvent = 
        EventManager.RegisterRoutedEvent("My", RoutingStrategy.Bubble, typeof(RadRoutedEventHandler), typeof(MainPage));

    Then you raise when needed and register for it with this code:

    this.RaiseEvent(new RadRoutedEventArgs(MyEvent, this));
    this.AddHandler(MyEvent, OnMyEvent);

    2. Create an attached property (behavior) and attach it to the custom user control. When the behavior is attached on the custom control it will register for this event. This means that the handler must be able to be a static method.
    We can help you with creating an attached behavior if you need.

    The feature about frozen or "floating" columns was actually implemented in the prototype of the control.

    The panel that floats the columns is even in this release:


    My suggestion though is to wait if possible until the Q1 release when the control will be updated, not it can serve mainly as a preview.

    Sincerely yours,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Florian Perrichot
    Florian  Perrichot avatar
    4 posts
    Member since:
    Oct 2009

    Posted 18 Nov 2009 Link to this post

    Thanks, it works great with 2 RoutedEvents and your answer is fast and accurate !

Back to Top