Je suis nouveau sur WPF et j'ai vu quelques bonnes vidéos sur MVVM et WPF, mais je suis confus quand j'essaie d'afficher une liste de données. Pour une raison quelconque, je n'ai pas de données dans ma liste. Voici ma classeMVVM Pattern Listview
Modèle/Person.cs
class Person : INotifyPropertyChanged
{
// 1. Fields, Constants
private int ID;
private string FirstName;
private string MiddleName;
private string LastName;
private DateTime Birthday;
private string Prefix;
private string Email;
public Person(int ID, string FirstName, string MiddleName, string LastName)
{
this.ID= ID;
this.FirstName = FirstName;
this.MiddleName = MiddleName;
this.LastName = LastName;
}
/// <summary>
/// Get or Sets the person first name
/// </summary>
public string firstName
{
get { return FirstName; }
set {
FirstName = value;
OnPropertyChanged("FirstName");
}
}
/// <summary>
/// Get or Sets the person middle name
/// </summary>
public string middleName
{
get { return MiddleName; }
set {
MiddleName = value;
OnPropertyChanged("MiddleName");
}
}
/// <summary>
/// Get or Sets the person last name
/// </summary>
public string lastName
{
get { return LastName; }
set {
LastName = value;
OnPropertyChanged("LastName");
}
}
/// <summary>
/// Get or Sets the person birthday
/// </summary>
public DateTime birthday
{
get { return Birthday; }
set {
Birthday = value;
OnPropertyChanged("Birthday");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
J'ai mon avis/Person.xaml.cs
public partial class Person : Window
{
public Person()
{
InitializeComponent();
lvDataBinding.DataContext = new PersonViewModel();
}
}
Le XAML est comme suit:
<ListView Margin="10" Name="lvDataBinding" ItemsSource="{Binding Person}" Grid.Column="1" Width="auto" Grid.ColumnSpan="8" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding firstName}" Header="First Name" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding lastName}" Header="Last Name" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding middleName}" Header="Middle Name" Width="100"/>
<GridViewColumn DisplayMemberBinding="{Binding Birthday}" Header="Birthday" Width="100"/>
</GridView>
</ListView.View>
</ListView>
Dernières pièces mais important ici est mon ViewModel/PersonViewModel.cs
internal class PersonViewModel
{
public ObservableCollection<Person> myData { get; set; }
public PersonViewModel()
{
List<Person> myData = new List<Person>();
}
public ObservableCollection<Person> getData()
{
myData.Add(new Person(12456, "John", "Francis", "Dufour"));
myData.Add(new Person(12456, "Heather", "Meagan", "Cornthwaite"));
if (myData.Count > 0)
{
return myData;
}
else
{
return null;
}
}
}
Je suis totalement du tableau ou est-ce quelque chose de très petit qui doit être réparé? Il suit également le modèle MVVM, et à l'avenir je prévois avoir accès à une base de données, ce modèle fonctionne-t-il bien pour moi dans le futur.
Au lieu de mettre à jour la question originale, ajoutez toujours la modification à la fin. Sinon, les réponses postées liées à votre question initiale perdront leur valeur. Vous modifiez complètement le code qu'il était plus tôt lorsque vous avez publié la question. –