2015-08-08 54 views
0

首先,如果這個問題沒有完美意義,請致歉 - 當談到C#和XAML時,我是一個完整的新手。如何設置XAML ListView並創建按鈕以更新值

我有一個創建了這個類人:

class Student 
{ 
    private string studentID; 
    public string StudentID 
    { 
     get { return studentID; } 
     set 
     { 
      studentID = value; 
      NotifyPropertyChanged("StudentID"); 
     } 
    } 

    private string firstName; 
    public string FirstName { 
     get { return firstName; } 
     set 
     { 
      firstName = value; 
      NotifyPropertyChanged("FirstName"); 
     } 
    } 

    private string surname; 
    public string Surname 
    { 
     get { return surname; } 
     set 
     { 
      surname = value; 
      NotifyPropertyChanged("Surname"); 
     } 
    } 

    private string group; 
    public string Group 
    { 
     get { return group; } 
     set 
     { 
      group = value; 
      NotifyPropertyChanged("Group"); 
     } 
    } 

    private int cValue; 
    public int CValue 
    { 
     get { return cValue; } 
     set 
     { 
      cValue = value; 
      NotifyPropertyChanged("CValue"); 
     } 
    } 

    private string teacher; 
    public string Teacher 
    { 
     get { return teacher; } 
     set 
     { 
      teacher = value; 
      NotifyPropertyChanged("Teacher"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged([CallerMemberName] string caller = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(caller)); 
     } 
    } 

    public Student() { } 

    public Student(string studentID, string firstName, string surname, string group, int cValue, string teacher) 
    { 
     StudentID = studentID; 
     FirstName = firstName; 
     Surname = surname; 
     Group = group; 
     CValue = cValue; 
     Teacher = teacher; 
    } 


    // strings used to create random students 
    private static readonly string[] firstNames = { "Adam", "Bob", "Carl", "David", "Edgar", "Frank", "George", "Harry", "Isaac", "Jesse", "Ken", "Larry" }; 
    private static readonly string[] surnames = { "Adamson", "Bobson", "Carlson", "Davidson", "Edgarson", "Frankson", "Georgeson", "Harryson", "Isaacson", "Jesseson", "Kenson", "Larryson" }; 
    private static readonly string[] groups = { "6a", "5b" }; 
    private static readonly string[] teachers = { "Fred", "Jim"}; 

    // method to create random students 
    public static IEnumerable<Student> CreateStudents(int count) 
    { 
     var people = new List<Student>(); 

     var r = new Random(); 

     for (int i=0; i< count; i++) 
     { 
      StringBuilder builder = new StringBuilder(); 
      builder.Append("A"); 
      builder.Append(i.ToString()); 
      string num = builder.ToString(); 
      var s = new Student() 
      { 
       StudentID = num, 
       FirstName = firstNames[r.Next(firstNames.Length)], 
       Surname = surnames[r.Next(surnames.Length)], 
       Group = groups[r.Next(groups.Length)], 
       Teacher = teachers[r.Next(teachers.Length)] 
      }; 
      people.Add(s); 
     } 
     return people; 
    } 

} 

我已經然後創建了這些人的對象列表,我可以此列表綁定到一個列表/網格視圖很輕鬆了。

我想要做的是在每個項目上有一個加號和減號按鈕,以便從該人員CValue中添加或帶走1。 (我會上傳一張圖片來演示,但是SO不會讓我......)

我該如何佈置XAML併爲此添加綁定,即使像我這樣的白癡也能理解?

謝謝!

回答

0

正如你可以看到here,PropertyChanged事件是INotifyPropertyChanged的接口事件,所以我認爲你的類'學生'需要明確地實現這個接口。

public class Student : INotifyPropertyChanged 

也許this實施指南可以幫助你。

0

您需要使用Observable Collection代替List

那麼所有你需要做的,只要你想被操縱這個名單,它會自動通知用戶界面與變化

0

您需要使用RelayCommand或您的學生虛擬機上的DelegateCommand。它們不是WPF的一部分,但都是ICommand的常用實現,它們直接在虛擬機中調用委託並在MVVM中廣泛使用。然後,您可以直接與您的按鈕Command對他們說:

class Student : INotifyPropertyChanged 
{ 
    ICommand AddCommand { get; private set; } 
    ICommand RemoveCommand { get; private set; } 

    public Student() 
    { 
     this.AddCommand = new RelayCommand(Add); 
     this.RemoveCommand = new RelayCommand(Remove); 
    } 

    private Add() 
    { 
     this.CValue++; 
    } 

    private Remove() 
    { 
     this.CValue--; 
    } 

    //snip rest of Student properties 
} 

而在你的XAML學生的模板:

<Button Command="{Binding AddCommand>" Content="Add"/> 
<Button Command="{Binding RemoveCommand>" Content="Remove"/> 

如果您希望將自己的Student對象不受VM的東西(即它純粹是一個模型類),那麼你可以實現你的母VM相同的命令,而是通過學生對象作爲命令參數:

class StudentsVM: INotifyPropertyChanged 
{ 
    ICommand AddCommand { get; private set; } 
    ICommand RemoveCommand { get; private set; } 

    public StudentsVM() 
    { 
     this.AddCommand = new RelayCommand<Student>(Add); 
     this.RemoveCommand = new RelayCommand<Student>(Remove); 
    } 

    private Add(Student student) 
    { 
     student.CValue++; 
    } 

    private Remove(Student student) 
    { 
     student.CValue--; 
    } 

    //snip rest of Student properties 
} 

而在你的XAML學生的模板:

<Button Command="{Binding DataContext.AddCommand, ElementName=root}" 
     CommandParameter="{Binding}" 
     Content="Add"/> 
<Button Command="{Binding DataContext.RemoveCommand, ElementName=root}" 
     CommandParameter="{Binding}" 
     Content="Remove"/> 

其中「根」是您的父視圖。這只是在父視圖中獲取命令的一種方法,如果您願意,可以使用RelativeSource。