• 依赖属性和数据绑定


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

    最近做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;

    }

    }
    }




  • 相关阅读:
    Angular 中自定义模块
    16 Angular【无人点餐无人收银系统案例】路由配置、菜品列表制作、请求数据渲染二维数组、 动态路由传值 、绑定html【基础项目
    13-angular中的路由
    Angular 互 中的数据交互 (get jsonp post )
    11-Rxjs异步数据流编程-Rxjs快速入门教程
    10 Angular中的生命周期函数--动态挂载销毁组件
    Angular 父子组件以及组件之间通讯
    Angular 中的 Dom 操作以及@ViewChild 、 Angular 执行 css3 动画
    Angular中的服务 以及自定义服务-数据持久化
    Stack与Queue
  • 原文地址:https://www.cnblogs.com/yanpo/p/2332071.html
Copyright © 2020-2023  润新知