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

  • Home
  • SEARCH
  • 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 3632014
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:28:17+00:00 2026-05-19T00:28:17+00:00

I have an ObservableCollection of objects as follows: public class UserDataViewModel { private ObservableCollection<CategoryItem>

  • 0

I have an ObservableCollection of objects as follows:

public class UserDataViewModel
{
  private ObservableCollection<CategoryItem> _data = 
                                       new ObservableCollection<CategoryItem>();

  public ObservableCollection<CategoryItem> Data
  {
    get { return _data; }
    private set { }
  }
  // Other methods to set Data
}

The CategoryItem class is defined as:

public class CategoryItem : INotifyPropertyChanged
{
  private string _name = null;
  private ObservableCollection<EntryItem> _entries = 
                                 new ObservableCollection<EntryItem>();

  public string Name
  {
    get { return _name; }
    set {
      if( value != _name ) {
        _name = value;
        NotifyPropertyChanged( "Name" );
      }
    }
  }

  public ObservableCollection<EntryItem> Entries
  {
    get { return _entries; }
    set {
      if( value != _entries ) {
        _entries = value;
        NotifyPropertyChanged( "Entries" );
      }
    }
  }
  // INotifyPropertyChanged code follows
}

The EntryItem class is defined as:

public class EntryItem : INotifyPropertyChanged
{
  private string _name = null;

  public string Name
  {
    get { return _name; }
    set {
      if( value != _name ) {
        _name = value;
        NotifyPropertyChanged( "Name" );
      }
    }
  }
  // INotifyPropertyChanged code follows
}

I’m trying to bind this to a ListBox. Each ListBoxItem consists of 2 TextBlocks. I want the first TextBlock to display the EntryItem.Name property and the second to display the CategoryItem.Name property. Here’s what I tried in XAML (without success):

<ListBox x:Name="MyListBox"
         Margin="0,0,-12,0"
         ItemsSource="{Binding Data}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Margin="0,0,0,17">
        <!--This should display EntryItem.Name-->
        <TextBlock Text="{Binding Entries.Name}"
                   TextWrapping="Wrap"
                   Margin="12,0,0,0"
                   Style="{StaticResource PhoneTextExtraLargeStyle}" />

        <!--This should display CategoryItem.Name-->
        <TextBlock Text="{Binding Name}"
                   TextWrapping="Wrap"
                   Margin="12,-6,0,0"
                   Style="{StaticResource PhoneTextSubtleStyle}" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

In the code-behind for this page I’m setting:

DataContext = App.ViewModel; // ViewModel is of type UserDataViewModel

I keep getting the binding error:

System.Windows.Data Error: BindingExpression path error: 'Name' property not found on 'System.Collections.ObjectModel.ObservableCollection`1[NestedCollection.ViewModels.EntryItem]' 'System.Collections.ObjectModel.ObservableCollection`1[NestedCollection.ViewModels.EntryItem]' (HashCode=123081170). BindingExpression: Path='Entries.Name' DataItem='NestedCollection.ViewModels.CategoryItem' (HashCode=121425257); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..

NestedCollection is the name of this project and all of the above classes are in the NestedCollection.ViewModels namespace.

Only the contents of the second TextBlock are being displayed. How do I fix this?

Thanks for your help, this has been driving me nuts for a few hours now!

EDIT:

Suppose the Data collection has 2 entries, “Credit Cards” and “Email Accounts” (these are Name property of each CategoryItem object in the collection. Say the first CategoryItem has the EntryItem objects “Visa”, “Mastercard” and “American Express”, and the second CategoryItem object has the EntryItem objects “GMail” and “Hotmail”, then I want the ListBox to display:

Visa
Credit Cards

Mastercard
Credit Cards

American Express
Credit Cards

GMail
Email Accounts

Hotmail
Email Accounts

I realize that the Entries property of Data does not have a Name property, each entry within it does. Is there anyway to index into the Entries in the XAML binding?

  • 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-05-19T00:28:18+00:00Added an answer on May 19, 2026 at 12:28 am

    You are trying to bind a ObservableCollection<T> to a TextBox. Think about it.

    ObservableCollection<EntryItem> does not have a property named Name. EntryItem class does.

    I suggest you use an ItemsControl instead or use a Converter to convert EntryItem names into a comma separated string.

    After looking at your edit:

    Try following code:

    <ListBox x:Name="MyListBox"
                Margin="0,0,-12,0"
                ItemsSource="{Binding Data}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Name="RootGrid">
                    <ItemsControl ItemsSource="{Binding Entries}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,17">
                                    <!--This should display EntryItem.Name-->
                                    <TextBlock Text="{Binding Name}"
                                                TextWrapping="Wrap"
                                                Margin="12,0,0,0"
                                                Style="{StaticResource PhoneTextExtraLargeStyle}" />
                                    <!--This should display CategoryItem.Name-->
                                    <TextBlock Text="{Binding ElementName=RootGrid, Path=DataContext.Name}"
                                                TextWrapping="Wrap"
                                                Margin="12,-6,0,0"
                                                Style="{StaticResource PhoneTextSubtleStyle}" />
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    –EDIT–

    Looks like it’s a known problem in Silverlight 3.

    http://forums.silverlight.net/forums/p/108804/280986.aspx

    To workaround that, either put a reference of CategoryItem in EntryItem named Parent or something similar to access it.

    public class EntryItem : INotifyPropertyChanged
    {
        public CategoryItem Parent { get; set; }
        ....
    }
    

    Or as discussed in the link above, put your DataTemplate into a UserControl for it to work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection similar to: Public Class MyCollection Inherits ObservableCollection(Of MyCollection) Private _Name
I have a class ShipmentsCollection that inherits ObservableCollection which contains shipment objects (entities). This
I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles
I have an ObservableCollection of Task objects. Each Task has the following properties: AssignedTo
I have an ObservableCollection of about 1000 objects that needs to be filtered (searched)
I have a MainViewModel and Customers property (list of CustomerViewModel objects as an ObservableCollection)
I have this ListBox which is bound to an ObservableCollection. Each object in the
I have a ComboBox bound to an ObservableCollection of decimals. What is the correct
I have an ItemsControl bound to an ObservableCollection. When the observable collection changes, I
I have WPF ListBox which is bound to a ObservableCollection, when the collection changes,

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.