Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MVVM中的C#WPF嵌套列表_C#_Wpf_List_Mvvm_Nested - Fatal编程技术网

MVVM中的C#WPF嵌套列表

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>

我想在MVVM中使用嵌套的ObservableCollection,以便添加尽可能多的组和用户。但是,我不知道如何创建/添加新用户。我也不知道如何将新用户绑定到XAML。 (注:这次我只需要两个人。)

我不熟悉C#、WPF和MVVM。 我正在学习有关此网站的MVVM: 我从上个星期就开始尝试了,但是没有成功

我试过:

        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));
}
}
}
}

。。。请帮帮我。提前谢谢。

我并不完全清楚结果应该是什么样子,但这里有一些初步建议

  • 我不会尝试将一个可观察的集合嵌套在另一个集合中。相反,可以定义一个单独的组模型类,该类的字段是用户对象列表

  • 我认为用户应该在xaml中输入绑定属性的值,以便创建新用户?您需要添加一个按钮或一些用户在填写完这些值后可以按下的东西。单击按钮应绑定到视图模型中的RelayCommand(如有必要,将MVVMLight添加到项目中)。所述relaycommand调用的方法将使用xaml中绑定的字段实例化一个新的用户对象,并添加到ObservableCollection中

  • “我也不知道如何将新用户绑定到XAML。”
  • 到目前为止,我还没有看到任何xaml代码能够处理显示新用户的问题。在我看来,您希望将用户集合绑定到网格或组合框。用户在文本框中输入新的用户属性并单击相应的按钮后,网格或组合框将更新。您可以为单独的组使用单独的控件。同样,这一部分我也不完全清楚

  • 给定(3),用户的ObservableCollection需要是实现OnPropertyChanged的视图模型中的属性
  • 希望能有所帮助。

    首先,欢迎来到C#、WPF和MVVM

    根据您的描述,听起来您希望显示组内用户的。。。考虑到这一点,您可以实现类似这样的目标:

    模型
    公共类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);       
    
        });