这篇随笔只是做个记录用。
最近做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;
}
}
}
来源:https://www.cnblogs.com/yanpo/archive/2012/01/30/2332071.html