再谈WPF
前几天初步看了一下WPF,按照网上说的一些方法,实现了WPF所谓的效果。但,今天我按照自己的思路设计了一个登陆界面,然后进行登陆验证,对WPF算是有进一步的理解,记录下来,以备后期查看。
首先,在WPF中,数据绑定很重要,也是WPF的核心。数据绑定和命令的绑定,需要后台来实现,主要是ViewModel来实现。
ViewModel中主要包含以下几项:
1、页面中所用到的参数,包括数据源和界面控件中的参数;这里的参数统一称为ViewModel的变量
2、ViewModel中需要定义一些命令参数来实现命令的绑定。数据的绑定统一绑定为ViewModel中的变量。
3、界面需要操作的变量属性,在ViewModel中封装时,要暴露给外面,在设置Set属性时,要进行特殊的处理。
4、在ViewModel中要实现界面按钮和其他需要的函数方法。
除了ViewModel外,还需要定义一个command类,继承于ICommand基类
下面是实现的核心代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input; namespace WpfApplication1
{
class LoginCommand:ICommand
{ private UserViewModelcs _userview; public LoginCommand(UserViewModelcs userviewmodel)
{
this._userview = userviewmodel;
} //ICommand 成员
public bool CanExecute(object parameter)
{
// throw new NotImplementedException();
return true;
} public event EventHandler CanExecuteChanged
{
add { }
remove{}
} public void Execute(object parameter)
{
//throw new NotImplementedException();
this._userview.login();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Input; namespace WpfApplication1
{
class UserViewModelcs:INotifyPropertyChanged
{ public event PropertyChangedEventHandler PropertyChanged;//INotifyPropertyChanged 成员 //
//查询的数据
private Collection<User> _DataList = null; //登录的命令
private ICommand _Lgoin = null; public ICommand Lgoin
{
get { return _Lgoin; }
} //登录框内容
private string username; public string Username
{
get { return username; }
set { username = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Username"));
}
}
}
private string pwd; public string Pwd
{
get { return pwd; }
set { pwd = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs("Pwd"));
}
}
} //构造函数
public UserViewModelcs(Collection<User> userlist)
{
this._DataList = userlist;
_Lgoin = new LoginCommand(this); } //登录方法
public void login()
{
if (this.Username == "aa")
{
if (this.Pwd == "aa")
{
System.Windows.MessageBox.Show("登录了");
} } }
}
}
再谈WPF的更多相关文章
- 浅谈WPF依赖项属性
浅谈WPF依赖项属性 0. 引言 依赖项属性虽然在使用上和CLR属性一样,但是它是WPF特有的,不同于CLR属性.只是封装为我们常用CLR的属性,在语法使用上和CLR属性一样.WPF中一些功能:动画, ...
- [转载]再谈百度:KPI、无人机,以及一个必须给父母看的案例
[转载]再谈百度:KPI.无人机,以及一个必须给父母看的案例 发表于 2016-03-15 | 0 Comments | 阅读次数 33 原文: 再谈百度:KPI.无人机,以及一个必须 ...
- Support Vector Machine (3) : 再谈泛化误差(Generalization Error)
目录 Support Vector Machine (1) : 简单SVM原理 Support Vector Machine (2) : Sequential Minimal Optimization ...
- Unity教程之再谈Unity中的优化技术
这是从 Unity教程之再谈Unity中的优化技术 这篇文章里提取出来的一部分,这篇文章让我学到了挺多可能我应该知道却还没知道的知识,写的挺好的 优化几何体 这一步主要是为了针对性能瓶颈中的”顶点 ...
- 浅谈WPF页间导航
浅谈WPF页间导航 使用导航的目的是从一个页面进入到另一个页面.无论是预先决定的线性顺序(向导)还是基于层次的用户驱动程序(大部分网站的形式),或者动态生成的路径,主要有3种方法实现:调用Naviga ...
- 浅谈HTTP中Get与Post的区别/HTTP协议与HTML表单(再谈GET与POST的区别)
HTTP协议与HTML表单(再谈GET与POST的区别) GET方式在request-line中传送数据:POST方式在request-line及request-body中均可以传送数据. http: ...
- Another Look at Events(再谈Events)
转载:http://www.qtcn.org/bbs/simple/?t31383.html Another Look at Events(再谈Events) 最近在学习Qt事件处理的时候发现一篇很不 ...
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...
- 再谈JSON -json定义及数据类型
再谈json 近期在项目中使用到了highcharts ,highstock做了一些统计分析.使用jQuery ajax那就不得不使用json, 可是在使用过程中也出现了非常多的疑惑,比方说,什么情况 ...
随机推荐
- PS 滤镜— — sparkle 效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- java-03 方法
#############练习###################### 1.键盘录入乘法表 import java.util.Scanner; public class PrintNN { pub ...
- Mysql源码学习——源码目录结构
目录清单 目录名 注释 Bdb 伯克利DB表引擎 BUILD 构建工程的脚本 Client 客户端 Cmd-line-utils 命令行工具 Config 构建工程所需的一些文件 Dbug Fred ...
- [转载]理解 I/O Completion Port (IOCP完成端口)
原文:理解 I/O Completion Port (IOCP完成端口)欢迎阅读此篇IOCP教程.我将先给出IOCP的定义然后给出它的实现方法,最后剖析一个Echo程序来为您拨开IOCP的谜云,除去你 ...
- XmlSerialize error: There was an error generating the XML document.
今天遇到一个很火的问题, 一个c#的class 序列化成xml后抛出异常, 信息为: XmlSerialize error: There was an error generating the XML ...
- 兼容ie6,ie7,ie8,firefox,chrome浏览器的代码片段
hack实现方式和原理 #hacker{ color:red; *color:white; /*for ie6,ie7*/ *+color:blue; /*for ie7*/ _color:gray; ...
- 八 Vue学习 fetch请求
1:import {login, getAdminInfo} from '@/api/getData'(从api/getData.js中import login函数.) 看一下如下的getData.j ...
- mongodb启动脚本
#!/bin/sh # #chkconfig: #description: mongodb start() { /usr/local/yunshipei/enterplorer/mongodb/bin ...
- asp.net mvc cookie超时返回登录页面问题
filterContext.HttpContext.Response.Write("<script>top.location.href = '/Login/Index';< ...
- Django ORM 那些相关操作
Django ORM 那些相关操作 一般操作 必知必会13条 <> all(): #查询所有的结果 <> filter(**kwargs) # 它包含了与所给筛选条件相匹配的对 ...