Listbox ItemsSource和DataContext在Silverlight 3中不绑定

时间:2009-09-01 07:35:24

标签: silverlight data-binding listbox

我有一个包含5个列表框的xaml导航页面(Silverlight 3)。四个列表框(状态列表)正确绑定ItemsSource和DataContext属性,但其中一个没有(主列表)。无论我是在xaml中进行绑定还是直接在代码隐藏中进行绑定(仅用于测试),对于那个列表框,ItemsSource和DataContext都保持为null。下面是我的ViewModel中的xaml和代码。有任何想法吗?

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 个答案:

答案 0 :(得分:1)

好吧,看看你的代码,在PersonPageViewModel中,你为所有变量创建新的ObservableCollection<Person>(),除了 personMasterList

这可能是你的问题。