依赖属性和数据绑定

这篇随笔只是做个记录用。

最近做WPF游戏项目,知道学好WPF一定要掌握的部分知识,因为手头不熟,所以需要将知识记录下来。

1.新建Silverlight项目

2.新建User类如下:

 

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;
using System.ComponentModel;

namespace SilverlightApplication2
{
public class User : Grid,INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name = string.Empty;
public string MyName
{
get
{
return _name;
}
set
{
_name = value;
if ( PropertyChanged != null )
{
//发送属性更改通知
PropertyChanged( this , new PropertyChangedEventArgs( "MyName" ) );
}
}
}
private string _pWD = string.Empty;
public string PWD
{
get
{
return _pWD;
}
set
{
_pWD = value;
if ( PropertyChanged != null )
{
//发送属性更改通知
PropertyChanged( this , new PropertyChangedEventArgs( "PWD" ) );
}
}
}


/// <summary>
/// 获取或设置X、Y坐标(依赖属性)
/// </summary>
public Point Coordinate
{
get { return ( Point )GetValue( CoordinateProperty ); }
set { SetValue( CoordinateProperty , value ); }
}
public static readonly DependencyProperty CoordinateProperty = DependencyProperty.Register(
"Coordinate" ,
typeof( Point ) ,
typeof( User ) ,
new PropertyMetadata( ChangeCoordinateProperty )
);
public static void ChangeCoordinateProperty( DependencyObject d , DependencyPropertyChangedEventArgs e )
{
//User user = d as User;
//user.MyName = "王五";
//user.PWD = "赵六";
//var a = user.Name;
//var b = user.PWD;
}
}
}


3.在MainPage.xaml中UI部分

<UserControl x:Class="SilverlightApplication2.MainPage"
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"
d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="#46461F" Loaded="LayoutRoot_Loaded">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="200*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="200*" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="txtName" Grid.Row="0" Grid.Column="0"
Foreground="White" Text="{Binding MyName}"></TextBlock>
<TextBlock x:Name="txtPWD" Grid.Row="1" Grid.Column="0"
Foreground="White" Text="{Binding PWD}"></TextBlock>
<Button x:Name="btnOK" Content="变了" Grid.Column="1" Click="btnOK_Click"></Button>
<TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding Coordinate}"
Name="txtCoord" />
</Grid>
</UserControl>

4.后台部分:

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;

namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage( )
{
InitializeComponent( );
}
User user = new User( );

private void btnOK_Click( object sender , RoutedEventArgs e )
{
user.MyName = "李";
user.PWD = "li";
//txtName.DataContext = user;
//txtPWD.DataContext = user;
user.Coordinate = new Point( 10 , 10 );
}

private void LayoutRoot_Loaded( object sender , RoutedEventArgs e )
{

user.MyName = "张";
user.PWD = "zhang";
user.Coordinate = new Point( 11,10);
txtName.DataContext = user;
txtPWD.DataContext = user;
txtCoord.DataContext = user;

}

}
}




posted on 2012-01-30 15:43 碧空雪狐 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/yanpo/archive/2012/01/30/2332071.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值