Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7776673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:56:35+00:00 2026-06-01T17:56:35+00:00

I have a datagrid nested inside the ItemTemplate of a ListBox. I’m trying to

  • 0

I have a datagrid nested inside the ItemTemplate of a ListBox. I’m trying to display a tree like data structure using this. My classes are as follows.

The object in my data context contains a List<Section> named Sections, my ListBox is bound to this. Each Section contains a List<Item> named Items, the DataGrid in eac ItemTemplate is bound to this.

When I run the app, I get a null reference exception from the XAML at the line with the binding. Is there a better/alternative way of doing this, or am I missing a trick with the binding?

<Window.Resources>
    <CollectionViewSource x:Key="SectionSource" /><!-- this is initialized and filled with an ObservableCollection<Section> Sections when the window loads-->
</Window.Resources>

<ListBox x:Name="lstIngredients" ItemsSource="{Binding Source={StaticResource SectionSource}}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DataTemplate.Resources>
                <CollectionViewSource x:Key="itemsSource" Source="{Binding Items}"/>
            </DataTemplate.Resources>

<DataGrid x:Name="dgItems" IsReadOnly="false" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" 
    DataContext="{Binding}"
    ItemsSource="{Binding Source={StaticResource Items}}"
    EnableRowVirtualization="false" 
    VirtualizingStackPanel.VirtualizationMode="Standard"
        <DataGrid.Columns>
<DataGridTemplateColumn Width="2*" Header="{lex:LocText ChickenPing.Shared:Strings:Measurement}">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock x:Name="quantity" Text="{Binding Measurement}" TextTrimming="CharacterEllipsis" TextAlignment="Left"/>
                    <!-- Null reference on this line caused by the binding. If I set this to any DependencyProperty on an Item object, I get a null reference-->
                </DataTemplate>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:56:36+00:00Added an answer on June 1, 2026 at 5:56 pm

    I eventually tracked this down to an event which was set in one of the TemplateColumns. Switching the event from

    <TextBlock x:Name="quantity" Text="{Binding Measurement}" GotFocus="txt_GotFocus" />

    to

    <Style x:Key="FocusableTextbox" TargetType="{x:Type TextBox}"> 
        <EventSetter Event="GotFocus" Handler="txt_GotFocus" /> 
    </Style>
    ...
    <TextBlock x:Name="quantity" Text="{Binding Measurement}" Style={StaticResource FocusableTextbox} />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have datagrid and inside this grid have template field. I add Header text
I have a datagrid getting bound to a dataset, and I want to display
I have a DataGrid where each column has a SortExpression. I would like the
I have a DataGrid, with an ItemTemplate that has an image and label. In
Here is the problem: I have datagrid binded with data acquired from web service
I have a DataGrid a user can add items to by entering data in
I have a datagrid bound to an ObservableCollection, and what I'd like to do
I have a DataGrid bound to a ViewModel's property of type ObservableCollection. Inside DataGrid
I have a WPF UserControl nested inside an ElementHost in a WinForm UserControl intended
I have a linkbutton that is nested in a datagrid that is nested in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.