views:

2953

answers:

1

I have a xaml navigation page with 5 listboxes (Silverlight 3). Four of the listboxes (the status lists) bind the ItemsSource and the DataContext properties correctly but one of them does not (the master list). Whether I do the bindings in xaml or directly in the code-behind (just to test), both ItemsSource and DataContext remain null for that one listbox. Below is the xaml and the code in my ViewModel. Any ideas?

PersonPage.xaml

<navigation:Page x:Class="PersonTracker.Views.PersonPage" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           xmlns:viewModel="clr-namespace:PersonTracking.ViewModels;assembly=PersonTracking"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Person Page">
    <navigation:Page.Resources>
        <viewModel:PersonPageViewModel x:Key="TheViewModel" d:IsDataSouce="True" />
        <DataTemplate x:Key="PersonMasterDataTemplate">
     <Grid d:DesignWidth="187" d:DesignHeight="42" Width="318" Height="23">
      <Grid.ColumnDefinitions>
       <ColumnDefinition Width="0.119*"/>
       <ColumnDefinition Width="0.214*"/>
       <ColumnDefinition Width="0.667*"/>
      </Grid.ColumnDefinitions>
      <TextBlock Text="{Binding Code}" TextWrapping="Wrap" d:LayoutOverrides="Width, Height" Grid.Column="0"/>
      <TextBlock Text="{Binding LastName}" TextWrapping="Wrap" d:LayoutOverrides="Width, Height" Grid.Column="1"/>
            </Grid>
       </DataTemplate>
        <DataTemplate x:Key="PersonSimpleDataTemplate" >
            <StackPanel Width="100" Height="100">
                <TextBlock Text="{Binding LastName}" />
            </StackPanel>
        </DataTemplate>
    </navigation:Page.Resources>
    <Grid x:Name="LayoutRoot">
       <Grid x:Name="ContentGrid" ShowGridLines="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <StackPanel Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" >
                <TextBlock Text="Legend" TextAlignment="Center" />
                <ListBox x:Name="PersonMasterList" ItemTemplate="{StaticResource PersonMasterDataTemplate}" ItemsSource="{Binding}" DataContext="{Binding Path=MasterPersons, Source={StaticResource TheViewModel}}" />
            </StackPanel>

            <StackPanel Grid.Column="1" Grid.Row="0" >
                <TextBlock Text="Sleeping" TextAlignment="Center" />
                <ListBox x:Name="SleepingList" ItemTemplate="{StaticResource PersonSimpleDataTemplate}" ItemsSource="{Binding}" DataContext="{Binding Path=SleepingPersons, Source={StaticResource TheViewModel}}" />
            </StackPanel>

            <StackPanel Grid.Column="2" Grid.Row="0" >
                <TextBlock Text="Eating" TextAlignment="Center" />
                <ListBox x:Name="EatingList" ItemTemplate="{StaticResource PersonSimpleDataTemplate}" ItemsSource="{Binding}" DataContext="{Binding Path=EatingPersons, Source={StaticResource TheViewModel}}" />
            </StackPanel>

            <StackPanel Grid.Column="3" Grid.Row="0" >
                    <TextBlock Text="Driving" TextAlignment="Center" />
                    <ListBox x:Name="DrivingList" ItemTemplate="{StaticResource PersonSimpleDataTemplate}" ItemsSource="{Binding}" DataContext="{Binding Path=DrivingPersons, Source={StaticResource TheViewModel}}" />
            </StackPanel>

            <StackPanel Grid.Column="4" Grid.Row="0" >
                <TextBlock Text="Working" TextAlignment="Center" />
                <ListBox x:Name="WorkingList" ItemTemplate="{StaticResource PersonSimpleDataTemplate}" ItemsSource="{Binding}" DataContext="{Binding Path=WorkingPersons, Source={StaticResource TheViewModel}}" />
            </StackPanel>
        </Grid>
    </Grid>
</navigation:Page>

Person.cs

public class Person : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public int PersonID { get; set; }

        private string code;
        public string Code 
        {
            get { return code; }
            set
            {
                code = value;
                OnPropertyChanged("Code");
            }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                OnPropertyChanged("LastName");
            }
        }

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}

PersonPageViewModel.cs

public class PersonPageViewModel
{
        private ObservableCollection<Person> masterPersons;
        public ObservableCollection<Person> MasterPersons
        {
            get
            {
                return masterPersons;
            }
        }

        private ObservableCollection<Person> sleepingPersons = new ObservableCollection<Person>();
        public ObservableCollection<Person> SleepingPersons            {
            get
            {
                return sleepingPersons;
            }
        }

        private ObservableCollection<Person> eatingPersons = new ObservableCollection<Person>();
        public ObservableCollection<Person> EatingPersons
        {
            get
            {
                return eatingPersons;
            }
        }

        private ObservableCollection<Person> drivingPersons = new ObservableCollection<Person>();
        public ObservableCollection<Person> DrivingPersons
        {
            get
            {
                return drivingPersons;
            }
        }

        private ObservableCollection<Person> workingPersons = new ObservableCollection<Person>();
        public ObservableCollection<Person> WorkingPersons
        {
            get
            {
                return workingPersons;
            }
        }

        public void LoadPeople()
        {
            // Get people from database
            // Add all people to master list
            // Add people to other lists based on status
        }
}
+1  A: 

Well, looking at your code, in PersonPageViewModel, you create new ObservableCollection<Person>() for all the variables except for the personMasterList

That might very well be your problem.

Johannes
Yeah, I just noticed that. That was a mistake based on some over zealous formatting/editing. I have fixed the code to what it is in the project.
Jacob Bartruff
Took a look at the solution this morning and came to realize that you were correct. I was not creating a new ObservableCollection like the others but I was assigning it to point to something later. Of course, this did not matter for the binding since the personMasterList collection was null when binding happened. I feel the fool. Thanks for pointing that out to me.
Jacob Bartruff