如何设置 XAML ListView 并创建按钮来更新值
How to set up XAML ListView and create buttons to update a value
首先,如果这个问题没有完全理解,我深表歉意 - 对于 C# 和 XAML。
我是一个完全的新手
我创建了这个 class 人:
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;
}
}
然后我创建了这些人物对象的列表,我可以很容易地将这个列表绑定到 list/grid 视图。
不过我想做的是在每个项目上有一个加号和减号按钮 以从那个人的 CValue 中添加或减去 1。 (我会上传图片来演示,但不会让我...)
我如何布置 XAML 并为此添加绑定,即使像我这样的白痴也能理解?
谢谢!
如你所见here,PropertyChanged事件是INotifyPropertyChanged的接口事件,所以我认为你的class'Student'需要显式实现这个接口。
public class Student : INotifyPropertyChanged
也许 this 实施指南可以帮助您。
您需要使用 Observable Collection 而不是 List
然后您所要做的就是根据需要操作此列表,它会自动将更改通知 UI
您需要在您的学生 VM 上使用 RelayCommand
或 DelegateCommand
。它们不是 WPF 的一部分,但都是 ICommand
的常用实现,可直接在您的 VM 中调用委托,并在 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 东西的影响(即它纯粹是一个模型 class),那么您可以在父 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"/>
其中 "root" 是您的父视图。这只是在父视图中获取命令的一种方式,如果您愿意,可以改用 RelativeSource。
首先,如果这个问题没有完全理解,我深表歉意 - 对于 C# 和 XAML。
我是一个完全的新手我创建了这个 class 人:
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;
}
}
然后我创建了这些人物对象的列表,我可以很容易地将这个列表绑定到 list/grid 视图。
不过我想做的是在每个项目上有一个加号和减号按钮 以从那个人的 CValue 中添加或减去 1。 (我会上传图片来演示,但不会让我...)
我如何布置 XAML 并为此添加绑定,即使像我这样的白痴也能理解?
谢谢!
如你所见here,PropertyChanged事件是INotifyPropertyChanged的接口事件,所以我认为你的class'Student'需要显式实现这个接口。
public class Student : INotifyPropertyChanged
也许 this 实施指南可以帮助您。
您需要使用 Observable Collection 而不是 List
然后您所要做的就是根据需要操作此列表,它会自动将更改通知 UI
您需要在您的学生 VM 上使用 RelayCommand
或 DelegateCommand
。它们不是 WPF 的一部分,但都是 ICommand
的常用实现,可直接在您的 VM 中调用委托,并在 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 东西的影响(即它纯粹是一个模型 class),那么您可以在父 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"/>
其中 "root" 是您的父视图。这只是在父视图中获取命令的一种方式,如果您愿意,可以改用 RelativeSource。