silverlight中依赖属性的应用

silverlight中依赖属性的应用,第1张

概述前台代码: <navigation:Page x:Class="dataBind.bindPerson"            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     

前台代码:

<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" name="textBox1" VerticalAlignment="top" WIDth="120" Text="{Binding name,Mode=TwoWay}" />
        <button Content="显示" Height="23" HorizontalAlignment="left" margin="157,191,0" name="button1" VerticalAlignment="top" WIDth="75" Click="button1_Click" />
        <button Content="设置" Height="23" HorizontalAlignment="left" margin="299,192,0" name="button2" VerticalAlignment="top" WIDth="75" Click="button2_Click" />
        <TextBox Height="23" HorizontalAlignment="left" margin="278,0" name="textBox2" VerticalAlignment="top" WIDth="120" Text="{Binding Age,Mode=TwoWay}" />
    </GrID>
</navigation:Page>

 

我们需要添加一个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,etc...
        public static Readonly DependencyProperty AgeProperty =
            DependencyProperty.Register("Age",typeof(int),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);
        }

    } }

总结

以上是内存溢出为你收集整理的silverlight中依赖属性的应用全部内容,希望文章能够帮你解决silverlight中依赖属性的应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1064832.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存