Silverlight使用Binding动态绑定数据

后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.ComponentModel;
namespace SilverlightApplication1
{
    public partial class Page2 : Page
    {
        private Person2 _p2 = new Person2();
        public Person2 P2
        {
            get { return _p2; }
            set { _p2 = value; }
        }
        public Page2()
        {
            InitializeComponent();
            this.DataContext = P2;
            //注册Page_Loaded事件
            
//Loaded += new RoutedEventHandler(Page_Loaded);
        }
        //private void Page_Loaded(object sender, EventArgs e)
        
//{
        
//    this.DataContext = P2;
        
//    P2.Age = 50;
        
//}
        
//设定模型属性值
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            int i = Convert.ToInt32(textBox2.Text);
            P2.Age = i;
        }
        //获取模型属性值
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("P2.Age属性值为"+P2.Age.ToString());
        }

    }
    //数据模型类
   public class Person2 :INotifyPropertyChanged 
    {
       //属性发生改变时事件
        public event PropertyChangedEventHandler PropertyChanged;
        private int _age;
        public int Age
        {
            get { return _age; }
            set 
            {
                _age = value;
                OnPropertyChanged("Age");
            }
        }
       //触发事件方法
        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(thisnew PropertyChangedEventArgs(property));
            }
        }
    }
}

 前台代码

<navigation:Page x:Class="SilverlightApplication1.Page2" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="400" d:DesignHeight="300"
           Title="Page2 Page">
    <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Button Content="Set(设定值)" Grid.Row="0" Grid.Column="0"  Height="23" HorizontalAlignment="Left" Margin="166,31,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <TextBox Height="23" Grid.Row="0" Grid.Column="0" Text="{Binding Age, Mode=OneTime}" HorizontalAlignment="Left" Margin="22,31,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <Button Content="Get(取值)" Height="23" HorizontalAlignment="Left" Margin="274,31,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="166,68,0,0" Name="textBox2" VerticalAlignment="Top" Width="68" />
    </Grid>
</navigation:Page>
 
效果如下:

Binding=TwoWay ; Set按钮直接给Age赋值为20,Get按钮取text随时变化的值

Binding Mode=Oneway时;Get取不到textbox随时输入的值,只能取Set按钮之后的值,

如果Binding Mode为OneTime时,直接得到Age初始化的值,按Set或Get TextBox的值不会变。

原文地址:https://www.cnblogs.com/Rmeo/p/2540350.html