2009-09-01 2 views
1

У меня есть страница навигации xaml с 5 списками (Silverlight 3). Четыре из списков (списки статуса) правильно связывают свойства ItemsSource и DataContext, но один из них не является (главный список). Я выполняю привязки в xaml или непосредственно в коде (только для проверки), как ItemsSource, так и DataContext остаются пустыми для этого одного списка. Ниже приведена xaml и код в моей модели ViewModel. Есть идеи?Listbox ItemsSource и DataContext не привязаны в 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 
     } 
} 

ответ

1

Ну, глядя на свой код, в PersonPageViewModel, вы создаете новый ObservableCollection<Person>() для всех переменные, за исключением personMasterList

Возможно, это будет ваша проблема.

+0

Да, я только что заметил это. Это была ошибка, основанная на некоторых из-за усердного форматирования/редактирования. Я исправил код до того, что он есть в проекте. –

+0

Сегодня утром взглянул на решение и понял, что вы были правы. Я не создавал новый ObservableCollection, как другие, но я назначал его, чтобы указать на что-то позже. Конечно, это не имело значения для привязки, поскольку коллекция personMasterList была нулевой, когда произошло переплет. Я чувствую себя дураком. Спасибо, что указали на это. –

Смежные вопросы