MVVM中的C#WPF嵌套列表
我想在MVVM中使用嵌套的ObservableCollection,以便添加尽可能多的组和用户。但是,我不知道如何创建/添加新用户。我也不知道如何将新用户绑定到XAML。 (注:这次我只需要两个人。) 我不熟悉C#、WPF和MVVM。 我正在学习有关此网站的MVVM: 我从上个星期就开始尝试了,但是没有成功 我试过:MVVM中的C#WPF嵌套列表,c#,wpf,list,mvvm,nested,C#,Wpf,List,Mvvm,Nested,我想在MVVM中使用嵌套的ObservableCollection,以便添加尽可能多的组和用户。但是,我不知道如何创建/添加新用户。我也不知道如何将新用户绑定到XAML。 (注:这次我只需要两个人。) 我不熟悉C#、WPF和MVVM。 我正在学习有关此网站的MVVM: 我从上个星期就开始尝试了,但是没有成功 我试过: outerObservableCollection.Add( new ObservableCollection<User>
outerObservableCollection.Add(
new ObservableCollection<User>
{
{
FirstName = "Jane",
LastName = "June",
BirthDate = DateTime.Now.AddYears(-20)
}
}
);
User.cs:
using System;
namespace MVVM_RIP_Tutorial
{
sealed class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
}
MyViewModel.cs:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace MVVM_RIP_Tutorial
{
// INotifyPropertyChanged notifies the View of property changes, so that Bindings are updated.
sealed class MyViewModel : INotifyPropertyChanged
{
private User user;
ObservableCollection<ObservableCollection<User>> outerObservableCollection
= new ObservableCollection<ObservableCollection<User>>();
//ObservableCollection<User> user = new ObservableCollection<User>();
public string FirstName
{
get { return user.FirstName; }
set
{
if (user.FirstName != value)
{
user.FirstName = value;
OnPropertyChange("FirstName");
// If the first name has changed, the FullName property needs to be udpated as well.
OnPropertyChange("FullName");
}
}
}
public string LastName
{
get { return user.LastName; }
set
{
if (user.LastName != value)
{
user.LastName = value;
OnPropertyChange("LastName");
// If the first name has changed, the FullName property needs to be udpated as well.
OnPropertyChange("FullName");
}
}
}
// This property is an example of how model properties can be presented differently to the View.
// In this case, we transform the birth date to the user's age, which is read only.
public int Age
{
get
{
DateTime today = DateTime.Today;
int age = today.Year - user.BirthDate.Year;
if (user.BirthDate > today.AddYears(-age)) age--;
return age;
}
}
// This property is just for display purposes and is a composition of existing data.
public string FullName
{
get { return FirstName + " " + LastName; }
}
public MyViewModel()
{
user = new User
{
FirstName = "John",
LastName = "Doe",
BirthDate = DateTime.Now.AddYears(-30)
};
//outerObservableCollection.Add(user);
//outerObservableCollection.Add(
// new ObservableCollection<User>
// {
// {
// FirstName = "Jane",
// LastName = "June",
// BirthDate = DateTime.Now.AddYears(-20)
// }
// }
//);
);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
名称空间MVVM_RIP_教程
{
//INotifyPropertyChanged通知视图属性更改,以便更新绑定。
密封类MyViewModel:INotifyPropertyChanged
{
私人用户;
ObservableCollection外部可观测集合
=新的ObservableCollection();
//ObservableCollection用户=新的ObservableCollection();
公共字符串名
{
获取{return user.FirstName;}
设置
{
if(user.FirstName!=值)
{
user.FirstName=值;
OnPropertyChange(“名字”);
//如果第一个名称已更改,则还需要使用FullName属性。
OnPropertyChange(“全名”);
}
}
}
公共字符串姓氏
{
获取{return user.LastName;}
设置
{
if(user.LastName!=值)
{
user.LastName=值;
OnPropertyChange(“姓氏”);
//如果第一个名称已更改,则还需要使用FullName属性。
OnPropertyChange(“全名”);
}
}
}
//此属性是模型属性如何以不同于视图的方式显示的示例。
//在本例中,我们将出生日期转换为用户的年龄(只读)。
公共信息
{
得到
{
DateTime today=DateTime.today;
int age=today.Year-user.BirthDate.Year;
如果(user.BirthDate>today.AddYears(-age))年龄--;
回归年龄;
}
}
//此属性仅用于显示目的,是现有数据的组成部分。
公共字符串全名
{
获取{return FirstName+“”+LastName;}
}
公共MyViewModel()
{
用户=新用户
{
FirstName=“约翰”,
LastName=“Doe”,
生日=DateTime.Now.AddYears(-30)
};
//outerObservableCollection.Add(用户);
//outerObservableCollection.Add(
//新可观测集合
// {
// {
//FirstName=“Jane”,
//LastName=“June”,
//生日=DateTime.Now.AddYears(-20)
// }
// }
//);
);
}
公共事件属性更改事件处理程序属性更改;
受保护的void OnPropertyChange(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
}
。。。请帮帮我。提前谢谢。我并不完全清楚结果应该是什么样子,但这里有一些初步建议
公共类GroupModel
{
公共组模型(uint id,字符串名称)
{
Id=Id;
名称=名称;
}
公共uint Id{get;}
公共字符串名称{get;}
}
公共类用户模型
{
公共用户模型(uint id、字符串名字、字符串姓氏、DateTime dateOfBirth)
{
Id=Id;
名字=名字;
姓=姓;
出生日期=出生日期;
}
公共uint Id{get;}
公共字符串名{get;}
公共字符串姓氏{get;}
public DateTime出生日期{get;}
}
视图模型
基类
公共抽象类ViewModelBase:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
[NotifyPropertyChangedInvocator]
受保护虚拟voi
using System;
namespace MVVM_RIP_Tutorial
{
sealed class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace MVVM_RIP_Tutorial
{
// INotifyPropertyChanged notifies the View of property changes, so that Bindings are updated.
sealed class MyViewModel : INotifyPropertyChanged
{
private User user;
ObservableCollection<ObservableCollection<User>> outerObservableCollection
= new ObservableCollection<ObservableCollection<User>>();
//ObservableCollection<User> user = new ObservableCollection<User>();
public string FirstName
{
get { return user.FirstName; }
set
{
if (user.FirstName != value)
{
user.FirstName = value;
OnPropertyChange("FirstName");
// If the first name has changed, the FullName property needs to be udpated as well.
OnPropertyChange("FullName");
}
}
}
public string LastName
{
get { return user.LastName; }
set
{
if (user.LastName != value)
{
user.LastName = value;
OnPropertyChange("LastName");
// If the first name has changed, the FullName property needs to be udpated as well.
OnPropertyChange("FullName");
}
}
}
// This property is an example of how model properties can be presented differently to the View.
// In this case, we transform the birth date to the user's age, which is read only.
public int Age
{
get
{
DateTime today = DateTime.Today;
int age = today.Year - user.BirthDate.Year;
if (user.BirthDate > today.AddYears(-age)) age--;
return age;
}
}
// This property is just for display purposes and is a composition of existing data.
public string FullName
{
get { return FirstName + " " + LastName; }
}
public MyViewModel()
{
user = new User
{
FirstName = "John",
LastName = "Doe",
BirthDate = DateTime.Now.AddYears(-30)
};
//outerObservableCollection.Add(user);
//outerObservableCollection.Add(
// new ObservableCollection<User>
// {
// {
// FirstName = "Jane",
// LastName = "June",
// BirthDate = DateTime.Now.AddYears(-20)
// }
// }
//);
);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChange(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
<Button Command="{Binding Path=CreateUserCommand}">
<TextBlock Text="Create User"/>
</Button>
public RelayCommand CreateUserCommand { get; private set; }
CreateUserCommand = new RelayCommand(() =>
{
User user = new User
{
FirstName = FirstName,
LastName = LastName,
//...etc.
}
collectionOfUsers.Add(user);
});