wpf mvvm 绑定(bind) View 到模型

标签 wpf data-binding mvvm view model

我正在尝试使用 MVVM。我创建了一个具有 3 个属性(ModelClass)的模型,在我的 View 模型中,我有一个可观察的 ModelClasses 集合。在 View 中,我将一个列表绑定(bind)到我的 VM 中的可观察集合。该列表是 3 个文本框的模板。

如何将文本框绑定(bind)到模型中的特定属性?

假设模型类名为 Person 并具有姓名、年龄和国家作为属性, View 模型具有人员集合, View 具有绑定(bind)到人员列表 (itemssource..) 的列表。我不知道如何在列表中创建一个与人物模型的姓名、年龄和国家/地区绑定(bind)的文本框。

最佳答案

根据您的查询,我构建了示例应用程序,它非常适合我。详细信息如下。

PersonViewModel.cs

  internal class PersonViewModel{

    public ObservableCollection<PersonModel> PersonCollection { get; set; }

    public PersonViewModel()
    {
        //This data will load as the default person from the model attached to the view
        PersonCollection = new ObservableCollection<PersonModel>();
        PersonCollection.Add(new PersonModel { Name = "John", Age= 24, Country = "Canada"});
        PersonCollection.Add(new PersonModel { Name = "David", Age = 25, Country = "United States"});
        PersonCollection.Add(new PersonModel { Name = "Prajin", Age = 28, Country = "Japan"});
    }
}

PersonView.xaml
<Window x:Class="MVVM.PersonView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MVVM Demostration" Height="297" Width="480"
    xmlns:local="clr-namespace:MVVM.ViewModel">
<Window.DataContext>
    <local:PersonViewModel />
</Window.DataContext>

<Grid Margin="10">
    <ListBox ItemsSource="{Binding PersonCollection}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Path=Name}" Margin="5" Grid.Column="0" FontSize="14" />                        
                    <TextBlock Text="{Binding Path=Age}" Margin="5" Grid.Column="1" FontSize="14" />
                    <TextBlock Text="{Binding Path=Country}" Margin="5" Grid.Column="2" FontSize="14"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

个人.cs
 internal class PersonModel : System.ComponentModel.INotifyPropertyChanged
{
    private string name;
    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    private int age;
    public int Age
    {
        get { return age; }
        set
        {
            age = value;
            OnPropertyChanged("Age");
        }
    }

    private string country;
    public string Country
    {
        get { return country; }
        set
        {
            country = value;
            OnPropertyChanged("Country");
        }
    }

    #region INotifyPropertyChanged Members

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

关于wpf mvvm 绑定(bind) View 到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6722361/

相关文章:

c# - C# 结构中的 INotifyPropertyChanged

data-binding - Xamarin 表单数据绑定(bind) "."分隔符

WPF MVVM 选项列表示例

wpf - WPF MVVM设计问题

android - 无法在Android MVVM中创建ViewModel类的实例

c# - 为Caliburn.Micro Action Message指定自定义防护属性

wpf - 设置全局字体系列

WPF:使用滚动条重新设置所有控件的样式

c# - 使用可观察对象使用数据库中的数据绑定(bind)更新数据网格

wpf - 在列表框中添加项目时将调用的事件处理程序