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

Reusable Duration Column

4 Answers 50 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrey
Top achievements
Rank 1
Andrey asked on 16 Mar 2011, 09:38 PM
I am trying to make reusable Duration column. Something like this:

public class GridViewDurationColumn : GridViewDataColumn
{
    public DateTime Start
    {
        get { return (DateTime)GetValue(StartProperty); }
        set { SetValue(StartProperty, value); }
    }
  
    public static readonly DependencyProperty StartProperty =
        DependencyProperty.Register("Start", typeof(DateTime), typeof(GridViewDurationColumn), new PropertyMetadata(null));
  
    public DateTime End
    {
        get { return (DateTime)GetValue(EndProperty); }
        set { SetValue(EndProperty, value); }
    }
  
    public static readonly DependencyProperty EndProperty =
        DependencyProperty.Register("End", typeof(DateTime), typeof(GridViewDurationColumn), new PropertyMetadata(null));
  
    public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
    {
        var duration = End - Start;
        return new TextBlock() { Text = duration.ToString() };
    }
  
}

<local:GridViewDurationColumn Start="{Binding StartDate}" End="{Binding EndDate}" IsReadOnly="True" />

but it doesn't work. Start and End properties are never set.
Please help! Thanks

4 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 16 Mar 2011, 10:08 PM
Hi Andrey,

 Columns in Silverlight are DependencyObjects and they don't automatically inherit the DataContext of the RadGridView. You need to expose your ViewModel as a static resource in your Silverlight page and reference it as the Source of the bindings of your Start and End properties on your custom column class.

Kind regards,
Yavor Georgiev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Andrey
Top achievements
Rank 1
answered on 16 Mar 2011, 11:08 PM
Thanks for quick reply!

but i want to get this Start and End property from each cell, not from my ViewModel.
0
Yavor Georgiev
Telerik team
answered on 17 Mar 2011, 03:44 AM
Hi Andrey,

 I'm afraid that your approach is wrong, in this case. Your Start and End properties need to be ordinary CLR properties, not dependency properties. Also, you will need to change their type to Binding.

 Then, in the CreateCellElement method, you will need to reflect your data item and get the properties defined in the two binding properties. Something like:
var itemType = dataItem.GetType();
 
var startPropertyInfo = itemType.GetProperty(this.Start.Path.Path);
var endPropertyInfo = itemType.GetProperty(this.End.Path.Path);
 
var startTime = (DateTime)startPropertyInfo.GetValue(dataItem, null);
var endTime = (DateTime)endPropertyInfo.GetValue(dataItem, null);
var duration = end - start;
 
return new TextBlock { Text = duration.ToString() };

Regards,
Yavor Georgiev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Andrey
Top achievements
Rank 1
answered on 17 Mar 2011, 09:59 PM
i just tried to make it in more straightforward way.
thanks, i should try this.
Tags
GridView
Asked by
Andrey
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
Andrey
Top achievements
Rank 1
Share this question
or