How do I get VS to populate my ListView data samples in the Design panel?
I'm at a dead end. I did a bit of reading and searching the Internet and did not find a reliable answer. Displaying fake data on the VS Design panel will speed it up significantly, since I don’t have to debug it every few changes.
Here is my code:
XAML:
<ListView DataContext="{Binding}" Name="PeopleListView"> <ListView.ItemTemplate> <DataTemplate DataType="Person"> <WrapPanel> <TextBlock Text="{Binding Path=FirstNameView}"/> <TextBlock Text="{Binding Path=LastNameView}" /> </WrapPanel> </DataTemplate> </ListView.ItemTemplate> </ListView>
COLLECTION:
public class People : ObservableCollection<Person> { public People() { this.Add(new Person() { FirstName = "John", LastName = "Doe" }); this.Add(new Person() { FirstName = "John", LastName = "Doe" }); this.Add(new Person() { FirstName = "John", LastName = "Doe" }); } }
HOME:
private People PList; public MainWindow() { this.PList = new People(); PeopleListView.ItemsSource = this.PList; }
EDIT and PROGRESS:
I read the tutorials found in the link provided by Matt West. They were very helpful. I almost figured it out.
I have a problem when Designer is trying to display Sample Data. I get this error for my sample properties: Writable property or collection expected . I understand the error, I just don’t know how to fix it without giving each property a set statement.
Here is my code:
Character class:
public class Person : INotifyPropertyChanged { private string _FirstName; private string _LastName; public string FirstName { get { return this._email; } set { this._FirstName = value; OnPropertyChanged("FirstName"); OnPropertyChanged("FirstNameView"); } } public string LastName { get { return this._password; } set { this._LastName = value; OnPropertyChanged("LastName"); OnPropertyChanged("FirstNameView"); } } public string FirstNameView { get { return this.FirstName; } } public string LastNameView { get { return this.LastName; } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { } }
PeopleViewSampleData.xaml:
<sample:People xmlns:sample="clr-namespace:MyProgram"> <sample:Person FirstNameView="John" LastNameView="Doe" /> </sample:People>
EDIT DECISION:
After installing ItemSource , DataContext and :DataContext setting made it work. If none of this was there, nothing would be visible.
<ListView ItemsSource="{Binding}" DataContext="{Binding}" d:DataContext="{d:DesignData Source=/SampleData/PeopleListViewSampleData.xaml}"/>