2009-09-01 8 views
1

J'ai une page de navigation xaml avec 5 zones de liste (Silverlight 3). Quatre des zones de liste (les listes d'état) lient correctement les propriétés ItemsSource et DataContext, mais pas l'une (la liste principale). Que je fasse les liaisons dans xaml ou directement dans le code-behind (juste pour tester), ItemsSource et DataContext restent null pour cette liste. Voici le xaml et le code dans mon ViewModel. Des idées?Listbox ItemsSource et DataContext ne sont pas liés dans Silverlight 3

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 
     } 
} 

Répondre

1

Eh bien, en regardant votre code, en PersonPageViewModel, vous créez ObservableCollection<Person>() pour tous les variables sauf le personMasterList

Cela pourrait très bien être votre problème.

+0

Oui, je viens de le remarquer. C'était une erreur basée sur un formatage/édition trop zélé. J'ai fixé le code à ce qu'il est dans le projet. –

+0

J'ai regardé la solution ce matin et je me suis rendu compte que vous aviez raison. Je ne créais pas une nouvelle ObservableCollection comme les autres mais je l'assignais pour pointer vers quelque chose plus tard. Bien sûr, cela n'a pas d'importance pour la liaison, car la collection personMasterList était nulle lors de la liaison. Je sens le fou. Merci de me l'avoir signalé. –

Questions connexes