第一个WPF应用程序
- WPF使用一种新的XAML(Extensible Application Markup Language)语言来开发界面,这将把界面开发以及后台逻辑很好的分开,降低了耦合度,使用户界面设计师与程序开发者能更好的合作,降低维护和更新的成本。这也使得应用不仅仅局限于winforme ,更可以移植到网页(HTML5)上,使得网页拥有客户端的效果。
2.全新的数据banding,使得开发起来更加容易
1.随着win8的出现,微软开始边缘化WPF/SilverLight而热捧HTML5。
2.但是WPF还是Windows上方便快捷地进行桌面应用开发的不错选择。同时Win8风格的应用也支持XAML。
总而言之,就是为了界面与逻辑分离



参考C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace Practice
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// 2016年4月11日 BY 伊甸一点
/// </summary> public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void ListView_Loaded(object sender, RoutedEventArgs e)//listview 加载时对gridview实现动态完成
{
GridView gridview = new GridView();
gridview.Columns.Add(new GridViewColumn { Header = "ID", DisplayMemberBinding = new Binding("Id") });
gridview.Columns.Add(new GridViewColumn { Header = "NAME", DisplayMemberBinding = new Binding("Name") });
gridview.Columns.Add(new GridViewColumn { Header = "CATEGORY", DisplayMemberBinding = new Binding("Category") });
listView1.View = gridview;
}
private void Button_Click(object sender, RoutedEventArgs e)//完成添加功能
{
string text1 = textBox1.Text;
string text2 = textBox2.Text;
string text3 = comoboBox1.Text.ToString();
Boolean flag = false;//进行标记,flag == false 说明ID都不重复, flag == true 说明ID有重复
if (text1 == "" || text2 == "" || text3 == "")
MessageBox.Show("Incomplete information", "Tips");//提示信息不完整
else
{
foreach (Book item in listView1.Items)//进行循环判断 item.id( Book的实例 )是否与listView1.Items的某个text1相等
{
if (text1 == item.Id)
{
MessageBox.Show("Already have same ID number", "Tips");//提醒已经有相同ID存在
flag = true;//修改flag
}
}
}
if (!flag)//相当于 if( flag == false )
listView1.Items.Add(new Book(text1, text2, text3));
} private void Button_Click_1(object sender, RoutedEventArgs e)//完成删除功能
{
if (listView1.SelectedItem == null)//判断是否选择中ListView中的某行
MessageBox.Show("Nothing have been choosed ", "Tips");
else
listView1.Items.Remove(listView1.SelectedItem);//删除选中的行
} }
class Book
{
public Book(string ID, string NAME, string CATEGORY)//构造函数
{
Id = ID;
Name = NAME;
Category = CATEGORY;
}
private string id;//封装的要求
//可以通过{ 右键--->重构--->封装字段 }实现自动完成get set函数
//下面相同
public string Id//再次使用id 时只需要调用Id即可
{
get { return id; }
set { id = value; }
}
private string name; public string Name
{
get { return name; }
set { name = value; }
}
private string category; public string Category
{
get { return category; }
set { category = value; }
}
}
}
参考XAML代码:
<Window x:Class="Practice.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="0,0,2,0">
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="40,240,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" Margin="192,240,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="0" Margin="119,276,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="4"/>
<ComboBox x:Name="comoboBox1" HorizontalAlignment="Left" Margin="352,240,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="文化" Height="23" Width="100"/>
<ComboBoxItem Content="科技" Height="23" Width="100"/>
<ComboBoxItem Content="软件" Height="23" Width="100"/>
</ComboBox>
<Button Content="Add" HorizontalAlignment="Left" Margin="134,276,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Button Content="Delete" HorizontalAlignment="Left" Margin="283,276,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<Grid Margin="40,10,43,139">
<ListView x:Name="listView1" HorizontalAlignment="Left" Height="170" VerticalAlignment="Top" Width="432" Loaded="ListView_Loaded">
<ListView.View>
<GridView/>
</ListView.View>
</ListView>
</Grid>
<Label Content="ID" HorizontalAlignment="Left" Margin="40,210,0,0" VerticalAlignment="Top"/>
<Label Content="Name" HorizontalAlignment="Left" Margin="192,210,0,0" VerticalAlignment="Top"/>
<Label Content="Category" HorizontalAlignment="Left" Margin="352,210,0,0" VerticalAlignment="Top"/> </Grid>
</Window>
对应的界面:

运行界面:

第一个WPF应用程序的更多相关文章
- 演练:我的第一个 WPF 桌面应用程序 https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/getting-started/walkthrough-my-first-wpf-desktop-application
这篇文章演示如何开发简单的 Windows Presentation Foundation (WPF) 应用程序包括元素所共有的大多数 WPF 应用程序: 可扩展应用程序标记语言 (XAML) 标记. ...
- WPF入门教程系列(一) 创建你的第一个WPF项目
WPF入门教程系列(一) 创建你的第一个WPF项目 WPF基础知识 快速学习绝不是从零学起的,良好的基础是快速入手的关键,下面先为大家摞列以下自己总结的学习WPF的几点基础知识: 1) C#基础语法知 ...
- WPF 之 WPF应用程序事件
当新建一个wpf应用程序,会自动生成一个App.xaml和MainWindow.xaml文件. 其中 App.xam 用来设置Application,应用程序的起始文件和资源及应用程序的一些属性和事件 ...
- wpf应用程序 打印标签
新建一个wpf应用程序,Xaml如下: <Window x:Class="CreateBarCodeDemo.MainWindow" xmlns="http://s ...
- 使用MVVM设计模式构建WPF应用程序
使用MVVM设计模式构建WPF应用程序 本文是翻译大牛Josh Smith的文章,WPF Apps With The Model-View-ViewModel Design Pattern,译者水平有 ...
- 运用Edraw为WPF应用程序嵌入Office文档的方法总结
具体描述了运用Edraw Office Viewer Component为WPF应用长须嵌入MS Word,Excel以及Power Point的方法. 打开Visual Studio,并创建一个新的 ...
- WPF应用程序顶级标签一定是Window吗?
原文:WPF应用程序顶级标签一定是Window吗? WPF应用程序顶级标签一定是Window吗? 很多人误以为是.可是,答案却是否定的.我们不妨来测试一下. 首先使用顶级标签为Window,这是最普通 ...
- 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)
搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...
- 写一个去除AI2XAML注释及多余数字位数的WPF窗体程序
原文:写一个去除AI2XAML注释及多余数字位数的WPF窗体程序 使用正则表达式去除多余注释及冗余数字位,关键代码: string pattern = @"/b(/d+ ...
随机推荐
- setTimeout,setInterval,process.nextTick,setImmediate in Nodejs
Nodejs的特点是事件驱动,异步I/O产生的高并发,产生此特点的引擎是事件循环,事件被分门别类地归到对应的事件观察者上,比如idle观察者,定时器观察者,I/O观察者等等,事件循环每次循环称为Tic ...
- Razor练习3
Razor语法中,物质循环处理,它使用到三种: for, while,foreach. 下面Insus.NET分别在ASP.NET MVC环境中列举一个例子,附加源代码: while:<br / ...
- MS SQL Server带有时间的记录怎样查询
比如某一张表[A]有一个保存日期包含时间字段[B],如果以这个段[B]作查询条件对数据记录进行查询.也我们得花些心思才能查询到我们想得到的记录. 现在我们需要查询这天2014-06-21的所有记录: ...
- ASP.NET MVC Model绑定小结
Model绑定是指从URL提取数据,生成对应Action方法的参数这个过程.前面介绍的一系列Descriptor负责提供了控制器,行为方法和参数的元数据,ValueProvieder负责获取数据,剩下 ...
- ExtJs动态生成treepanel的Json格式
在节点中加上"checked"属性,会自动生成checkbox. 获取选中节点 var nodeArray = ""; var nodesObj = mytre ...
- xpath学习积累
"//comment()":“所有注释节点”
- 算法实例-C#-快速排序-QuickSort
算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...
- 我的JavaScript笔记
JavaScript 一种基于对象(object)和事件驱动(Event Driven)的嵌入式脚本语言. 简单的例子 <html> <head> <title>D ...
- JS json的使用
json的定义 json能够通过4种基本数据类型以及2种结构化数据表示 字符串 "footbar" 不能使用单引号 数值 125.4 只支持10进制 布尔 true fals ...
- [LeetCode]String to Integer (atoi)
题意:字符串转正数 原题来自:https://leetcode.com/problems/string-to-integer-atoi/ 分析: <程序员面试宝典>上出现的面试题,主要是考 ...