大家都知道:UI是做好一个软件很重要的因素,如果没有一个漂亮的UI,功能做的再好也无法吸引很多用户使用,而且没有漂亮的界面,那么普通用户会感觉这个软件没有多少使用价值。

WPF系统基于流布局的标准,开发人员可以建立与分辨率和窗口大小无关的界面UI,在不同显示器上可以得到自由缩放。在开发过程中应该注意以下原则:

1,不需要设置元素尺寸大小
2,不需要使用坐标指定元素位置
3,嵌套布局容器
4,一个窗口包含一个元素

控件很多,不需要个个都精确学会

今天来掌握几个简单的东西,Button按钮,Label标签,TextBox文本,PasswordBox密码,RadioButton单选,首先在项目中创建文件夹images,复制几个图片到文件夹中。

第一步,在xaml文件中添加如下代码

<Window x:Class="WpfApp6.MainWindow"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"xmlns:local="clr-namespace:WpfApp6"mc:Ignorable="d"Title="WPF基础控件" Height="758.275" Width="1078.5" Icon="images/22i.jpg" WindowStartupLocation="CenterScreen"  Loaded="Window_Loaded"><Grid Name="mygrid" ><Button Name="btnlogin"  IsDefault="True" Click="btnlogin_Click" Background="YellowGreen" BorderThickness="3"  BorderBrush="Red" Content="登录"  HorizontalAlignment="Left" Margin="166,95,0,0" VerticalAlignment="Top" Width="74" RenderTransformOrigin="-0.2,0.368"/><Label Content="帐号" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="54,23,0,0" VerticalAlignment="Top" Width="102"/><TextBox Name="txtname" HorizontalAlignment="Left" Height="23" Margin="142,23,0,0" TextWrapping="Wrap" Text="admin" VerticalAlignment="Top" Width="120"/><Label Content="密码" HorizontalAlignment="Left" Margin="276,23,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.088,0.6"/><PasswordBox Name="txtpass" HorizontalAlignment="Left" Height="22" Margin="330,23,0,0"  Password="123456" PasswordChar="#" VerticalAlignment="Top" Width="120"/><Button Name="btncancle" IsCancel="True" Foreground="Chartreuse"  FontSize="14" Click="btncancle_Click" Content="取消" HorizontalAlignment="Left" Margin="266,95,0,0" VerticalAlignment="Top" Width="74"><Button.Background><ImageBrush  ImageSource="images/32s.jpg"></ImageBrush></Button.Background></Button><RadioButton Content="老师"  GroupName="role" HorizontalAlignment="Left" Margin="156,65,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/><RadioButton Content="学生"  GroupName="role" HorizontalAlignment="Left" Margin="230,65,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.14,0.357" Checked="RadioButton_Checked"/><RadioButton Content="管理员"   GroupName="role" HorizontalAlignment="Left" Margin="303,65,0,0" VerticalAlignment="Top" Checked="RadioButton_Checked"/></Grid>
</Window>

所有控件都可以在左侧窗口中找到

说明下:

1、设置了窗口的图标,启动位置在正中间,启动事件中动态添加一个单选按钮

2、Grid设置了名称, Name="mygrid",Button设置了背景,RadioButton设置了分组,控件都设置了Content表示显示内容,Name表示控件名称,这在后台代码可能需要用到,所以设置了名称,HorizontalAlignment表示水平对齐,Foreground表示前景色,Margin表示左上右下边距,跟CSS的盒子模型意思一样,Click表示事件名称,这些属性名称很多,不一一说明,这些东西跟CSS里的东西含义一样,所以证明了这个xaml文件是布局文件

第二步,在.cs文件中写逻辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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 WpfApp6
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void btncancle_Click(object sender, RoutedEventArgs e){}private void btnlogin_Click(object sender, RoutedEventArgs e){string uname = txtname.Text.Trim();string passwd = txtpass.Password.Trim();MessageBox.Show("登录成功","登录提示",MessageBoxButton.OK,MessageBoxImage.Information);}private void RadioButton_Checked(object sender, RoutedEventArgs e){string t = (sender as RadioButton).Content.ToString();MessageBox.Show(t);}private void Window_Loaded(object sender, RoutedEventArgs e){//动态添加一个单选按钮RadioButton radbtn = new RadioButton();radbtn.Content = "主任";radbtn.GroupName = "role";radbtn.HorizontalAlignment = HorizontalAlignment.Left;radbtn.VerticalAlignment = VerticalAlignment.Top;radbtn.Margin = new Thickness(375,64, 0, 0);radbtn.Checked += RadioButton_Checked;//指定事件处理mygrid.Children.Add(radbtn); }}
}

第三步,运行程序,效果是

别小看这些演练,那可是万丈高楼拔地而起,一步步堆砌,就是摩天楼。

效果还可以,看到了炫彩,确实眼前一亮,比winform厉害得多,得加鸡腿了!

WPF真入门教程04--UI布局1相关推荐

  1. WPF真入门教程02--新建WPF工程

    在VS开发环境安装完成之后,首先我们先新建一个WPF工程,然后对工程目录结构啥的要有所了解才行. 打开VS2019 工程建好之后,WPF应用程序"会在"引用"里面自动添加 ...

  2. WPF真入门教程22--样式应用

    1.什么是样式 WPF相较于以前学的WinForm,WPF在UI设计与动画方面的炫丽是最吸引我来学习的.在WPF中XMAL代码的引入使得代码的编写能够前后端分离,为获得更好的界面,也使得我们不得不分出 ...

  3. WPF真入门教程03--XAML介绍

    一.窗体类基本概念 对于WPF应用程序,在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类.用户通过窗口与 Windows ...

  4. WPF真入门教程23--MVVM简单介绍

    在WPF开发中,经典的编程模式是MVVM,是为WPF量身定做的模式,该模式充分利用了WPF的数据绑定机制,最大限度地降低了Xmal文件和CS文件的耦合度,也就是UI显示和逻辑代码的耦合度,如需要更换界 ...

  5. WPF真入门教程20--数据排序

    如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会成为数据源 ...

  6. WPF真入门教程12--ListView控件

    ListView 控件在Windows应用程序中常用,用于表示数据列表.如果您以前使用过 WinForms,那么您对ListView的实用性有一个很好的了解,但您应该意识到 WPF中的ListView ...

  7. WPF真入门教程01--WPF简介

    Windows Presentation Foundation (简称 WPF),WPF是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的 ...

  8. WPF真入门教程21--WPF资源系统

    WPF资源系统是一种保管一系列对象(如常用的画刷.样式或模版)的简单办法,从而使您更容易地复用这些对象. WPF允许在代码中以及在标记中的各个位置定义资源(如特定的控件或窗口或在整个应用程序中定义). ...

  9. WPF案例 (六) 动态切换UI布局

    原文:WPF案例 (六) 动态切换UI布局 这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTempla ...

最新文章

  1. KMP算法的理解,伪代码,c代码实现
  2. 3月上旬中国域名解析服务商TOP10 DNSPOD升至8.24%
  3. ubuntu20上安装starUML3
  4. manjaro21 linux安装vscode
  5. 再见丑陋的 Swagger,这个API神器界面更炫酷,逼格更高,体验更好!
  6. TensorFlow(一)
  7. python数据接口设计_Python接口与归一化设计
  8. vb.net怎么调用fastreport报表_财务分析-企业财务管理报表模板制作实现智能化的财务运营...
  9. CSS 制作垂直导航
  10. UNIX/Linux系统取证之信息采集案例
  11. Expression Blend学习5控件
  12. 词云中去重复的词_今天从零教你开始利用Python打造词云图!
  13. 所谓的AI芯片到底指什么?
  14. xp系统重装后无法连接服务器6,winXP重装系统后不能上网的解决方法
  15. 丑数java_丑数-Java研发军团
  16. 小米刷入Recovery
  17. word只读模式怎么改成编辑模式
  18. java返回一个布尔值_关于java:返回布尔值的方法
  19. 微信小程序 Basic Auth 前后端restful api进行身份验证
  20. 推荐几个Sublime插件

热门文章

  1. [转帖]FPGA开发工具汇总
  2. 笔记本win10系统插入耳机没声音
  3. JSP内置对象及常用方法
  4. 微信公众号爬虫项目(reptile)
  5. sql 查询 同一手机号的 姓名 相同 的所有学生的信息
  6. 做芯片设计需要学编程吗?
  7. 【CV】SiamFC:用于目标跟踪的全卷积孪生网络
  8. 怎么修改QQ空间发说说显示的机型
  9. 2010年程序员工资
  10. java虚拟机32位_jre1.6java虚拟机运行环境下载|jre1.6官方版32位/64位下载_v1.6.0_9号软件下载...