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

Binding to sub objects using cell template

2 Answers 74 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bartłomiej Tyla
Top achievements
Rank 1
Bartłomiej Tyla asked on 06 Apr 2012, 11:22 PM

Hello

I'm developing silverlight application using wcf data service and radgridview. From wcf dataservice I get collection of objects TimesheetWeekSummary. 

[DataContract]
  public class TimesheetWeekSummary
  {
      [DataMember]
      public WorkingDay[] WorkingDays;
 
      // other properties
 
      [DataContract]
      public class WorkingDay
      {
          [DataMember]
          public DateTime Date { get; set; }
          [DataMember]
          public decimal? Hours { get; set; }
          [DataMember]
          public bool IsApproved { get; set; }
      }
  }

Now I want to bind each object of WorkingDays array to different RadGridView columns, corresponding to each day of the week (WorkingDays contains 7 elements). Fyi I'm creating columns dynamically. In each cell I want to display Date in one line and Hours in another. I've written a template to do so:

<DataTemplate x:Name="DayCellTemplate" x:Key="DayCell">
    <StackPanel Orientation="Vertical">
        <TextBlock Text="{Binding Date, StringFormat='yyyy/MM/dd' }" />
        <TextBlock Text="{Binding Hours}" />
    </StackPanel>
</DataTemplate>

And here is example of how the columns are created:

WorkingDayConverter converter = new WorkingDayConverter();
radGridView.Columns.Add(new GridViewDataColumn()
{
    UniqueName = Columns.Monday.ToString(),
    Header = columnHeaders[Columns.Monday],
    DataMemberBinding = new System.Windows.Data.Binding() { Converter = converter, ConverterParameter = (int)DayOfWeek.Monday },
    CellTemplate = (DataTemplate)LayoutRoot.Resources["DayCell"],
    Width = 100,
    IsResizable = false,
    IsReadOnly = true
});

Converter code seems fine:

public class WorkingDayConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        int param = (int)parameter;
        if (value == null || param < 0 || param > 6)
            return null;
 
        TimesheetWeekSummary weekSummary = (TimesheetWeekSummary)value;
        return weekSummary.WorkingDays[param];
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

This solution is unfortunately not working, grid cells are empty and the object binded to the data template is TimesheetWeekSummary (I can display it's properties if I change the template).

Any help would be greatly appreciated. 

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 07 Apr 2012, 01:48 PM
Hello,

The data context of inside the cell would be your TimeSheetWeekSummary , so the correct binding should point to the nested property ... something like :

<TextBlock Text="{Binding WorkingDay.Hours}" />  rather than "{Binding Hours}"
Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Bartłomiej Tyla
Top achievements
Rank 1
answered on 07 Apr 2012, 08:53 PM
Yes, but that's why I'm using the converter. I think I didn't explain it properly. I'd like the converter to bind the correct WorkingDay on a fly. I'm passing a parameter to the converter ((int)DayOfWeek.Monday for instance) and it does return correct WorkingDay, but the data context of the cell template is still the TimesheetWeekSummary, just as you said. 
Tags
General Discussions
Asked by
Bartłomiej Tyla
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Bartłomiej Tyla
Top achievements
Rank 1
Share this question
or