<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>
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);
}
} }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。