我有一個類型項目的觀察集合顯示的ObservableCollection
我的主窗口。 XAML
<ListView Name="ListViewProjects" Grid.Column="0" Grid.RowSpan="3" SelectionChanged="ListViewProjectsSelectionChanged" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" MinWidth="100">
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Path=ProjectID}"/>
<TextBlock Text="{Binding Path=ProjectName}"/>
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我MainWindow.cs
public partial class MainWindow : Window
{
ObservableCollection<Project> Projects = new ObservableCollection<Project>();
ObservableCollection<Employee> Employees = new ObservableCollection<Employee>();
public MainWindow()
{
InitializeComponent();
DataContext = Projects;
Project pro1 = new Project(1, "Swordfish");
Projects.Add(pro1);
Employee empMads = new Employee("Mads", 1);
Employee empBrian = new Employee("Brian", 2);
Employees.Add(empMads);
Employees.Add(empBrian);
}
private void ListViewProjectsSelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
和我Project.cs它是類文件
[Serializable]
class Project : INotifyPropertyChanged
{
public Project(int id, string name)
{
ID = id;
Name = name;
}
private int id;
public int ID
{
get { return id; }
set
{
id = value;
NotifyPropertyChanged("ProjectID");
}
}
private string name;
public string Name
{
get { return name; }
set
{
name = value;
NotifyPropertyChanged("ProjectName");
}
}
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
但是沒有東西被添加到我的清單我不能看到我缺少它的工作。
我把它作爲一個的ObservableCollection 我做的DataContext =收集 而且我在XAML文件結合
編輯codepart 1:
public ObservableCollection<Project> Projects { get; set; }
public ObservableCollection<Employee> Employees { get; set; }
public MainWindow()
{
InitializeComponent();
Projects = new ObservableCollection<Project>();
Employees = new ObservableCollection<Employee>();
DataContext = Projects;
想想你可以發佈你如何設置你的DataContext? – Josh 2011-01-13 14:04:47