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

Add event to datatemplate in code-behind

2 Answers 475 Views
TreeListView
This is a migrated thread and some comments may be shown as answers.
Florian Perrichot
Top achievements
Rank 1
Florian Perrichot asked on 13 Nov 2009, 06:49 AM
Hello,

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); 
                dateList.Add(date); 
            } 
        } 
 
        public DataTemplate CreateDataTemplate(int index) 
        { 
            string xamlstring = @"
            <DataTemplate
            xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
            xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
            xmlns:local='clr-namespace:WorklogApplication.Controls;assembly=WorklogApplication'>
                <Grid>
                    <local:WorklogControl Text='{Binding Path=Works[" + index + @"].ElapsedTime, Mode=OneWay}' Value='{Binding Path=Works[" + index + @"], Mode=OneWay}' />
                </Grid>
            </DataTemplate>"
            DataTemplate dt = XamlReader.Load(xamlstring) as DataTemplate; 
            dt.LoadContent(); 
            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.Columns.Add(column); 
            workLogTree.LayoutUpdated +=new EventHandler(workLogTree_LayoutUpdated); 
        } 
 
        void workLogTree_LayoutUpdated(object sender, EventArgs e) 
        { 
            AddEvents(); 
        } 
 
        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) 
            { 
                worksCollection.Add(BuildWorkLog(project)); 
            } 
 
            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.

Thanks

Regards,

Florian

2 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav
Telerik team
answered on 18 Nov 2009, 06:36 PM
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:

Telerik.Windows.Controls.TreeList.FloatPanel;

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,
Miroslav
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.
0
Florian Perrichot
Top achievements
Rank 1
answered on 18 Nov 2009, 08:25 PM
Thanks, it works great with 2 RoutedEvents and your answer is fast and accurate !

Florian
Tags
TreeListView
Asked by
Florian Perrichot
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Florian Perrichot
Top achievements
Rank 1
Share this question
or