silverlight中依赖属性的应用

前台代码:

<navigation:Page x:Class="dataBind.bindPerson"
           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="640" d:DesignHeight="480"
           Title="bindPerson Page">
    <Grid x:Name="LayoutRoot">
        <TextBox Height="23" HorizontalAlignment="Left" Margin="133,122,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Name,Mode=TwoWay}" />
        <Button Content="显示" Height="23" HorizontalAlignment="Left" Margin="157,191,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Content="设置" Height="23" HorizontalAlignment="Left" Margin="299,192,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="278,122,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Age,Mode=TwoWay}" />
    </Grid>
</navigation:Page>

 silverlight中依赖属性的应用_第1张图片

我们需要添加一个person类:

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace dataBind
{
    public class person:DependencyObject
    {
       private string name;

        //public string Name
        //{
        //    get { return name; }
        //    set { name = value; }
        //}


        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            set { SetValue(NameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty NameProperty =
            DependencyProperty.Register("Name", typeof(string), typeof(person),null );

       
        private int age;

        //public int Age
        //{
        //    get { return name; }
        //    set { name = value; }
        //}

        public int Age
        {
            get { return (int)GetValue(AgeProperty); }
            set { SetValue(AgeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Age.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AgeProperty =
            DependencyProperty.Register("Age", typeof(int), typeof(person),null );

       
      
    }
}

 

后台代码:

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;

namespace dataBind
{
    public partial class bindPerson : Page
    {
        public bindPerson()
        {
            InitializeComponent();
        }
        person p = new person() { Name = "张三", Age = 27 };

        // 当用户导航到此页面时执行。
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
           
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox1.DataContext = p;
            textBox2.DataContext = p;
            MessageBox.Show("姓名:" + p.Name + "\n年龄:" + p.Age);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            p.Name = "安亭";
            p.Age = 23;
            MessageBox.Show("姓名:" + p.Name + "\n年龄:" + p.Age);
        }

    }
}

 


 

 



原文链接: http://blog.csdn.net/mypc2010/article/details/7951000

你可能感兴趣的:(silverlight中依赖属性的应用)