C# 如何在视图和ViewModel之间传递数据
我是MVVM设计模式的新手,我正在尝试创建一个简单的应用程序,其中学生列表显示在主窗口中,我希望用户能够将新学生添加到列表中我已经完成了学生数据所在的可观察集合的绑定,但我如何通过从文本框并将其用作命令中的参数 这是我的看法C# 如何在视图和ViewModel之间传递数据,c#,wpf,mvvm,command,C#,Wpf,Mvvm,Command,我是MVVM设计模式的新手,我正在尝试创建一个简单的应用程序,其中学生列表显示在主窗口中,我希望用户能够将新学生添加到列表中我已经完成了学生数据所在的可观察集合的绑定,但我如何通过从文本框并将其用作命令中的参数 这是我的看法 <Grid> <Grid.RowDefinitions> <RowDefinition Height="2*"/> <RowDefinition Height="2*"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="NameTextBlock"
Text="Name"
Style="{StaticResource TextBlockTextStyle}"/>
<TextBlock x:Name="SurnameTextBlock"
Grid.Row="1"
Text="Surname"
Style="{StaticResource TextBlockTextStyle}"/>
<TextBlock x:Name="AgeTextBlock"
Grid.Row="2"
Text="Age"
Style="{StaticResource TextBlockTextStyle}"/>
<TextBox x:Name="NameTextBox"
Grid.Column="1"
Style="{StaticResource TextBoxTextStyle}"/>
<TextBox x:Name="SurnameTextBox"
Grid.Row="1"
Grid.Column="1"
Style="{StaticResource TextBoxTextStyle}"/>
<TextBox x:Name="AgeTextBox"
Grid.Row="2"
Grid.Column="1"
Style="{StaticResource TextBoxTextStyle}"/>
<ListBox x:Name="StudentListBox"
Grid.ColumnSpan="2"
Grid.Row="4"
Style="{StaticResource ListBoxStyle}"
ItemsSource="{Binding StudentList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Name}"
Style="{StaticResource TextBlockTextStyle}"/>
<TextBlock Text="{Binding Surname}"
Grid.Column="1"
Style="{StaticResource TextBlockTextStyle}"/>
<TextBlock Text="{Binding Age}"
Grid.Column="2"
Style="{StaticResource TextBlockTextStyle}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="AddButton"
Grid.Row="7"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
Content="Add"
Margin="7,7,7,7"
Command="{Binding AddStudentCommand}"/>
</Grid>
这是我的视图模型
public class MainViewModel : ViewModelBase
{
ObservableCollection<Student> studentList;
public MainViewModel()
{
//populate some sample data
studentList = new ObservableCollection<Student>()
{
new Student(){Name="John", Surname="Smith", Age="28"},
new Student(){Name="Barbara", Surname="Anderson", Age="23"}
};
}
public ObservableCollection<Student> StudentList
{
get { return studentList; }
set { RaisePropertyChanged("studentList"); }
}
Student selectedPerson;
public Student SelectedPerson
{
get { return selectedPerson; }
set
{
selectedPerson = value;
RaisePropertyChanged("SelectedPerson");
}
}
private RelayCommand _addStudentCommand;
public ICommand AddStudentCommand
{
get
{
return _addStudentCommand
?? (_addStudentCommand = new RelayCommand(() =>
{
Student student = new Student();
// here should be the logic of defining the name, surname,
// age and id of the newly created student
studentList.Add(student);
}));
}
}
}
public类MainViewModel:ViewModelBase
{
可观察收集学生名单;
公共主视图模型()
{
//填充一些示例数据
studentList=新的ObservableCollection()
{
新生(){Name=“John”,姓氏=“Smith”,年龄=“28”},
新学生(){Name=“Barbara”,姓氏=“Anderson”,年龄=“23”}
};
}
公众可观察的集合学生名单
{
获取{return studentList;}
集合{RaisePropertyChanged(“studentList”);}
}
学生选择人;
公立学生选择人
{
获取{返回selectedPerson;}
设置
{
selectedPerson=值;
RaiseProperty变更(“选定人员”);
}
}
私有RelayCommand _addStudentCommand;
公共ICommand AddStudentCommand
{
得到
{
return\u addStudentCommand
??(_addStudentCommand=新的RelayCommand(()=>
{
学生=新生();
//这里应该是定义名字,姓氏,
//新创建的学生的年龄和id
学生列表。添加(学生);
}));
}
}
}
我在当前项目中使用MVVMLight,有很多事情我不理解,因此请解释我应该如何传递文本框的数据,以及在使用它的命令中应该发生什么
如果需要,请告诉我添加更多代码 我要做的是在ViewModel中创建一些文本框绑定到的属性。 这三种情况(在viewmodel中)都是这样的 然后,在XAML中,将文本框的文本绑定到以下内容:
<TextBox x:Name="NameTextBox"
Text="{Binding Name}"
Grid.Column="1"
/>
(从我的评论中厚颜无耻地发布)
正如您所说,您对MVVM没有太多的经验,也不太了解,因此,尽管我可以回答这个问题,但我认为我能做的最好的事情是为您提供一些链接来观看:
及
第二个视频来自MVVM light的作者
它们基本上涵盖了相同的材料,但都带来了稍微不同的视角 嘿:)正如你所说的,你没有太多的经验,也不太了解MVVM,所以尽管我可以回答这个问题,但我认为我能做的最好的事情是为你提供一些链接来观看:和。第二个视频来自MVVM light的作者。它们基本上涵盖了相同的材料,但都带来了稍微不同的视角@不朽的蓝色谢谢你的视频,它帮助了很多习惯的力量。如果要将更改传播回业务对象,通常需要双向绑定。我编辑了我的帖子并省略了双向绑定。这回答了你的问题吗?是的,谢谢你的代码,但我这里有一个问题-如果我想创建一个不同的类来保存我的数据,我应该如何在任何ViewModels和commandsWell中使用它,我只是给你一个基本的示例,说明什么是可行的。你必须给出一个更具体的例子来说明你想要达到的目标。WPF非常灵活,你有很多选择。当然可以!如果我回答了您的问题,请务必将其标记为已回答:)
<TextBox x:Name="NameTextBox"
Text="{Binding Name}"
Grid.Column="1"
/>
private RelayCommand _addStudentCommand;
public ICommand AddStudentCommand
{
get
{
return _addStudentCommand
?? (_addStudentCommand = new RelayCommand(() =>
{
Student student = new Student();
student.Name = this.Name;
student.Surname = this.Surname;
student.Age = this.Age;
// here should be the logic of defining the name, surname,
// age and id of the newly created student
_StudentList.Add(student);
}));
}
}