【问题标题】:How to pass data between the View and the ViewModel如何在 View 和 ViewModel 之间传递数据
【发布时间】:2013-11-30 22:26:24
【问题描述】:

我是 MVVM 设计模式的新手我正在尝试创建一个简单的应用程序,其中学生列表显示在主窗口中,我希望用户能够将新学生添加到列表中我已经完成了学生数据所在的可观察集合的绑定,但是如何通过从文本框中获取数据并将它们用作命令中的参数来创建新用户

这是我的观点

<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>

这是我的 ViewModel

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

}

我在当前项目中使用 MVVMLight,有很多我不明白的地方,所以请解释我应该如何传递文本框的数据以及在使用它的命令中究竟应该发生什么。

如有必要,请告诉我添加更多代码。

【问题讨论】:

  • 嘿 :) 正如你所说,你对 MVVM 没有太多经验,也不太了解,所以虽然我可以回答这个问题,但我认为我能做的最好的事情为您提供一些观看链接:youtube.com/watch?v=BClf7GZR0DQchannel9.msdn.com/blogs/kreekman/…。第二个视频来自编写 MVVM-light 的人。它们基本上涵盖了相同的材料,但都带来了略微不同的视角!
  • @ImmortalBlue 感谢视频提供了很多帮助

标签: c# wpf mvvm command


【解决方案1】:

我要做的是在 ViewModel 中创建一些文本框绑定到的属性。 对所有三个都这样(在您的视图模型中)

private string _Name;
    public string Name
    {
        get { return _Name; }
        set
        {
            _Name = value;
            RaisePropertyChanged("Name");
        }
    }

然后,在 XAML 中,将文本框的文本绑定到此:

<TextBox x:Name="NameTextBox"
             Text="{Binding Name}"
         Grid.Column="1"
         />

最后在 AddStudent 命令中引用绑定到文本框的视图模型中的属性。

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

【讨论】:

  • 只是习惯的力量。如果要将更改传播回业务对象,通常需要双向绑定。我编辑了我的帖子并省略了双向绑定。
  • 这回答了你的问题吗?
  • 是的,谢谢你的代码,但我有一个问题 - 如果我想创建一个不同的类来保存我的数据,我应该如何在任何 ViewModel 和命令中使用它
  • 好吧,我只是给你一个基本的例子来说明什么是可行的。你必须给出一个更具体的例子来说明你想要达到的目标。 WPF 非常灵活,您有很多选择。
  • 当然可以!如果我回答了您的问题,请务必将其标记为已回答 :)
【解决方案2】:

(从我的评论中厚颜无耻地发布)

正如您所说,您没有太多经验,也不太了解 MVVM,所以虽然我可以回答这个问题,但我认为我能做的最好的事情就是为您提供一些观看链接:

youtube.com/watch?v=BClf7GZR0DQ

channel9.msdn.com/blogs/kreekman/

第二个视频来自编写 MVVM-light 的人。

它们基本上涵盖了相同的材料,但都带来了略微不同的视角!

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 2019-01-12
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2014-09-23
    • 2014-05-06
    相关资源
    最近更新 更多