DataGrid binding issue

May 15, 2010 at 7:03 PM

Hi,

I am a beginner trying to find my way with wpf, datagrid etc ...... I have this in my code

Xaml:

.....

 

xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit

<dg:DataGrid AutoGenerateColumns="False"  Name="SessionTable" ItemsSource="{Binding}"  Width="Auto">
                <dg:DataGrid.Columns>
                    <dg:DataGridTemplateColumn Header="Session No" MinWidth="100">
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding SessionNo}"/>
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>
                    <dg:DataGridTemplateColumn Header="Start Time" MinWidth="100"  >
                        <dg:DataGridTemplateColumn.CellTemplate >
                            <DataTemplate>
                                <my:ClockTimeDisplay Name="StartTime"/>
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>
                    <dg:DataGridTemplateColumn Header="Stop Time" MinWidth="100">
                        <dg:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <my:ClockTimeDisplay Name="StopTime"  />
                            </DataTemplate>
                        </dg:DataGridTemplateColumn.CellTemplate>
                    </dg:DataGridTemplateColumn>
                </dg:DataGrid.Columns>
            </dg:DataGrid>
     

....

<UserControl x:Class="mei.pcs.shared.ClockTimeDisplay"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto" Width="Auto">
    <StackPanel Orientation="Horizontal" Margin="5">
        <TextBox Name="tbHours" Text="{Binding Path=Hours}"/>
        <Label Content=":"/>
        <TextBox Name="tbMinutes" Text="{Binding Path=Minutes}"/>
        <Label Content=":"/>
        <TextBox Name="tbSeconds" Text="{Binding Path=Seconds}"/>

    </StackPanel>
</UserControl>


Code Behind:

 

public class ClockTime : IEditableObject
{
public String Hours{get ;set ; }
public String Minutes{get ;set ; }
public String Seconds{get ;set ; }

......

}

 

    public class Session : IEditableObject, INotifyPropertyChanged
    {
        public Session(int no, string start, string stop);

        public int SessionNo { get; set; }
        public ClockTime StartTime { get; set; }
        public ClockTime StopTime { get; set; }
    }
....
    public class SessionCollection : ObservableCollection<Session>
    {
        public SessionCollection();
    }

....
SessionTable.DataContext = sessionCollection;
 
Since I am setting the datacontext of the datagrid to an ObservableCollection I assume the binding must be as follows:
1. Each row of the datagrid is bound to a Session object in the collection
2. Columns of the rows are bound to the properties of the Session object ..
i.e 1st column is bound to an int(i.e SessionNo), 2nd and 3rd to ClockTime objects.

Is this understanding correct ? If yes, then the UserControl "ClockTimeDisplay" used in the CellTemplate must have its datacontext set to a ClockTime object
and the Textboxes used in the UserControl must display the values of the Hours ,Minutes and Seconds.

But only the SessionNo is being displayed and not the ClockTime data. Where am I going wrong ? Any suggestion would help.

Thanks in advance
Sagar.