silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了silverlight中依赖属性的应用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

前台代码: <navigation:Page x:Class="dataBind.bindPerson"            xmlns="http://scheR_77_11845@as.microsoft.com/winfx/2006/xaml/presentation"            xmlns:x="http://scheR_77_11845@as.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>

 

silverlight中依赖属性的应用

我们需要添加一个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);
        }

    } }

@H_248_262@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。