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

DataContext issue with HierarchyChildTemplate

0 Answers 22 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ryan
Top achievements
Rank 1
Ryan asked on 29 Oct 2013, 10:28 PM
I have a RadGridView in which the ItemsSource is bound to an ObservableCollection of "Incident" objects. The XAML also defines a RadGridView.HierarchyChildTemplate so the RadGridView will show a "+" next to each row allowing the user to expand each row to edit additional information about the Incident. Since the code I'm working on is proprietary, I've created a watered down sample of that code, which is shown below:

Here is the xaml:

   <TelerikGridView:RadGridView ItemsSource="{Binding Incidients}" x:Name="radGridViewMain">
 
            <TelerikGridView:RadGridView.ChildTableDefinitions>
                <TelerikGridView:GridViewTableDefinition>
                    <TelerikGridView:GridViewTableDefinition.Relation>
                        <TelerikWindowsData:PropertyRelation ParentPropertyName="IncidientID"/>
                    </TelerikGridView:GridViewTableDefinition.Relation>
                </TelerikGridView:GridViewTableDefinition>
            </TelerikGridView:RadGridView.ChildTableDefinitions>
 
 
            <TelerikGridView:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <Grid DataContextChanged="Grid_DataContextChanged">
                        <TextBox Text="{Binding CommentsText, Mode=TwoWay}" ></TextBox>                       
                    </Grid>
 
                </DataTemplate>
            </TelerikGridView:RadGridView.HierarchyChildTemplate>
 
                                                <TelerikGridView:RadGridView.Columns>
                                                    <TelerikGridView:GridViewDataColumn x:Uid="gridViewDataColumnUnitCode" Header="Unit Code" DataMemberBinding="{Binding UnitCode}" IsFilterable="False"/>
                                                    <TelerikGridView:GridViewDataColumn x:Uid="gridViewDataColumnStatus" Header="Status" DataMemberBinding="{Binding AssignmentStatusNameDisplay}" IsFilterable="False"/>
                                                    <TelerikGridView:GridViewDataColumn x:Uid="gridViewDataColumnLocation" Header="Location" DataMemberBinding="{Binding Location}"  IsFilterable="False"/>
                                                    <TelerikGridView:GridViewDataColumn x:Uid="gridViewDataColumnETA" Header="ETA" DataMemberBinding="{Binding RelativeETAFromNowDisplay}" IsFilterable="False"/>
                                                    <TelerikGridView:GridViewDataColumn x:Uid="gridViewDataColumnElapsed" Header="Elapsed" DataMemberBinding="{Binding ElapsedTimeDisplay}" IsFilterable="False"/>
                                                </TelerikGridView:RadGridView.Columns>
 
 
            <TelerikGridView:RadGridView.SortDescriptors>
                <TelerikControlsGridView:ColumnSortDescriptor  x:Uid="columnSortDescriptorPriorityNumber" Column="{Binding Columns[\IncidientID
\], ElementName=radGridViewMain}"
                                            SortDirection="Ascending" />
 
            </TelerikGridView:RadGridView.SortDescriptors>
</TelerikGridView:RadGridView>


Below is a copy of what the event handler looks like in the code-behind:

private void Grid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
  System.Diagnostics.Debugger.Break();
}


The following is the issue I'm having:

The first time the row is expanded, the DataContextChanged handler is hit, as expected (i.e. e.OldValue is null, and e.NewValue is set to an instance of Incident), which, is correct. The problem is that every time a new item is added to the Incidents (ObservableCollection<Incident>) object, to which the grid is bound, the DataContextChanged event gets fired twice for the row that is currently expanded.

Here is what the values are the first time it's hit:
e.OldValue = <Incident>
e.NewValue = NULL

The second time:
e.OldValue = NULL
e.NewValue = <Incident>


The only time the DataContextChanged handler should fire is when the user initially expands the row. Moreover, even if it did fire a second time when a new row is added to the collection, why does it nullify it, just to put it back to what it was before?

What would cause this to happen?



No answers yet. Maybe you can help?

Tags
GridView
Asked by
Ryan
Top achievements
Rank 1
Share this question
or