1
我有一个包含5个列表框(Silverlight 3)的xaml导航页面。其中四个列表框(状态列表)正确地绑定了ItemsSource和DataContext属性,但其中一个没有(主列表)。无论我在xaml中执行绑定还是直接在代码后台(仅测试),对于该列表框,ItemsSource和DataContext都保留为空。下面是我的ViewModel中的xaml和代码。有任何想法吗?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
}
}
是的,我刚刚注意到了。这是基于一些过分热心的格式化/编辑的错误。我已经将代码固定在项目中。 – 2009-09-01 08:13:37
今天早上看了一下解决方案,发现你是对的。我没有像其他人那样创建一个新的ObservableCollection,但我将它指派给稍后的某个东西。当然,绑定无关紧要,因为绑定发生时personMasterList集合为null。我感到愚蠢。谢谢你指出我。 – 2009-09-01 17:29:02