I have ObservableCollectionobjects as follows:
public class UserDataViewModel
{
private ObservableCollection<CategoryItem> _data =
new ObservableCollection<CategoryItem>();
public ObservableCollection<CategoryItem> Data
{
get { return _data; }
private set { }
}
}
A class is CategoryItemdefined 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" );
}
}
}
}
A class is EntryItemdefined as:
public class EntryItem : INotifyPropertyChanged
{
private string _name = null;
public string Name
{
get { return _name; }
set {
if( value != _name ) {
_name = value;
NotifyPropertyChanged( "Name" );
}
}
}
}
I am trying to relate this to ListBox. Each ListBoxItemconsists of 2 TextBlocks. I want the first to TextBlockdisplay the property EntryItem.Nameand the second to display the property CategoryItem.Name. Here is 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">
<TextBlock Text="{Binding Entries.Name}"
TextWrapping="Wrap"
Margin="12,0,0,0"
Style="{StaticResource PhoneTextExtraLargeStyle}" />
<TextBlock Text="{Binding Name}"
TextWrapping="Wrap"
Margin="12,-6,0,0"
Style="{StaticResource PhoneTextSubtleStyle}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In the code for this page, I install:
DataContext = App.ViewModel; // ViewModel is of type UserDataViewModel
I keep getting a 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')..
NestedCollectionis the name of this project, and all the classes listed above are in the namespace NestedCollection.ViewModels.
Only the contents of the second are displayed TextBlock. How to fix it?
Thanks for your help, it made me go for hours!
EDIT:
, Data 2 : " " " " ( Name CategoryItem ). , CategoryItem EntryItem "Visa", "Mastercard" "American Express", CategoryItem EntryItem "GMail" "Hotmail", , ListBox :
Visa
Credit Cards
Mastercard
Credit Cards
American Express
Credit Cards
GMail
Email Accounts
Hotmail
Email Accounts
, Entries Data Name, . Entries XAML?