MVVM模式

1.定义实体,实现接口:INotifyPropertyChanged和INotifyPropertyChanging

public class Course : INotifyPropertyChanged, INotifyPropertyChanging
        {
        
            private int _id;
            public int Id
            {
                get { return _id; }
                set
                {
                    if (_id != value)
                    {
                        RaisePropertyChanging("Id");
                        _id = value;
                        RaisePropertyChanged("Id");
                    }
                }
            }

            private string _name;
            public string Name
            {
                get { return _name; }
                set
                {
                    if (_name != value)
                    {
                        RaisePropertyChanging("Name");
                        _name = value;
                        RaisePropertyChanged("Name");
                    }
                }
            }

            private string _location;
            public string Location
            {
                get { return _location; }
                set
                {
                    if (_location != value)
                    {
                        RaisePropertyChanging("Location");
                        _location = value;
                        RaisePropertyChanged("Location");
                    }
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void RaisePropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }

            public event PropertyChangingEventHandler PropertyChanging;

            private void RaisePropertyChanging(string propertyName)
            {
                if (PropertyChanging != null)
                {
                    PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
                }
            }
        }
2.页面绑定数据

 <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0.5*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="课程名称" Grid.Row="0" Grid.Column="0"/>
            <TextBox Text="{Binding Name,Mode=TwoWay}" Grid.Column="1"/>
            <TextBlock Text="上课地点" Grid.Row="1" Grid.Column="0"/>
            <TextBox Text="{Binding Location,Mode=TwoWay}" Grid.Row="1" Grid.Column="1"/>

           
        </Grid>


3.页面后台代码绑定上下文

  // 构造函数
        public MainPage()
        {
            InitializeComponent();
            Course c = new Course()
            {
                Name = "电子商务",
                Location = "教学楼403"
            };
           // this.DataContext = c; 当前页面绑定上下文
            this.ContentPanel.DataContext = c; //Grid空间绑定上下文 
        }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值