1

Can't bind collection property to data grid row template

<DataTemplate x:Key="RowDetailsTemplate" DataType="{x:Type summons:IndividualDefendantSearchViewModel}">

            <controls:ExtendedDataGrid
                x:Name="RowDetails"
                CanUserSortColumns="True"
                ContentWidth="910"
                DataContext="{Binding ElementName=Parent, Path=DataContext.GroupedSearchResults}"
                Focusable="False"
                HeadersVisibility="None"
                IsSynchronizedWithCurrentItem="False"
                ItemsSource="{Binding Path=SelectableIndividualDefendantSearchResults}"
                ShowRowSelection="False">

                <controls:ExtendedDataGrid.Columns>             
                    <DataGridTextColumn
                        Width="*"
                        Binding="{Binding Name}"
                        Header="{localization:LanguageBinding ResourceKey=Name}" />
                </controls:ExtendedDataGrid.Columns>
            </controls:ExtendedDataGrid>
        </DataTemplate>

My Data Template

public class SelectableIndividualDefendantSearchResult : CheckedListItem<SearchResult>
{

    public string Name
        => $"{Item.FirstName} {(string.IsNullOrEmpty(Item.Insertion) ? "" : Item.Insertion + " ")}{Item.LastName}";


}

public class GroupedSelectableIndividualDefendantSearchResult: EnhancedBindableBase
{

    public DateTime? SearchDate { get; set; }

    public ObservableCollection<SelectableIndividualDefendantSearchResult>
        _selectableIndividualDefendantSearchResults;


    public ObservableCollection<SelectableIndividualDefendantSearchResult>
        SelectableIndividualDefendantSearchResults {
        get { return _selectableIndividualDefendantSearchResults; }
        set { SetProperty(ref _selectableIndividualDefendantSearchResults, value); }
    }

}

My property:

public ObservableCollection<GroupedSelectableIndividualDefendantSearchResult> GroupedSearchResults
    {
        get { return _groupedSearchResults; }
        set { SetProperty(ref _groupedSearchResults, value); }
    }

Main DataGrid ItemSource is

ItemsSource="{Binding GroupedSearchResults}"

I can see SearchDate bound to main rows, but RowDataTemplate is empty string... What i am doing wrong?

Alex Wyler
  • 65
  • 8

2 Answers2

0

Don't bind the DataContext to {Binding ElementName=Parent, Path=DataContext.GroupedSearchResults}:

<DataTemplate x:Key="RowDetailsTemplate">
    <controls:ExtendedDataGrid
                x:Name="RowDetails"
                CanUserSortColumns="True"
                ContentWidth="910"
                Focusable="False"
                HeadersVisibility="None"
                IsSynchronizedWithCurrentItem="False"
                ItemsSource="{Binding Path=SelectableIndividualDefendantSearchResults}"
                ShowRowSelection="False">

        <controls:ExtendedDataGrid.Columns>
            <DataGridTextColumn
                        Width="*"
                        Binding="{Binding Name}"
                        Header="{localization:LanguageBinding ResourceKey=Name}" />
        </controls:ExtendedDataGrid.Columns>
    </controls:ExtendedDataGrid>
</DataTemplate>
mm8
  • 163,881
  • 10
  • 57
  • 88
0

The type of the DataContext of your DataTemplate is IndividualDefendantSearchViewModel. So your ItemsSource binding points to IndividualDefendantSearchViewModel.SelectableIndividualDefendantSearchResults, right?

You have to bind to another DataContext where the SelectableIndividualDefendantSearchResults property is located. You can do this with the following:

{Binding DataContext.SelectableIndividualDefendantSearchResults, RelativeSource={RelativeSource AncestorType=UserControl}}

The UserControl holds the DataContext with the target property and you need to adapt it for your purpose (if it's different from UserControl).

Addition:

mm8 is right. do not bind to the DataContext of your DataGrid, there is no need.

S. Spindler
  • 546
  • 3
  • 12