WPF真入门教程04--UI布局1
大家都知道: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相关推荐
- WPF真入门教程02--新建WPF工程
在VS开发环境安装完成之后,首先我们先新建一个WPF工程,然后对工程目录结构啥的要有所了解才行. 打开VS2019 工程建好之后,WPF应用程序"会在"引用"里面自动添加 ...
- WPF真入门教程22--样式应用
1.什么是样式 WPF相较于以前学的WinForm,WPF在UI设计与动画方面的炫丽是最吸引我来学习的.在WPF中XMAL代码的引入使得代码的编写能够前后端分离,为获得更好的界面,也使得我们不得不分出 ...
- WPF真入门教程03--XAML介绍
一.窗体类基本概念 对于WPF应用程序,在Visual Studio和Expression Blend中,自定义的窗体均继承System.Windows.Window类.用户通过窗口与 Windows ...
- WPF真入门教程23--MVVM简单介绍
在WPF开发中,经典的编程模式是MVVM,是为WPF量身定做的模式,该模式充分利用了WPF的数据绑定机制,最大限度地降低了Xmal文件和CS文件的耦合度,也就是UI显示和逻辑代码的耦合度,如需要更换界 ...
- WPF真入门教程20--数据排序
如果想以特定的方式对数据进行排序,可以绑定到 CollectionViewSource,而不是直接绑定到 ObjectDataProvider.CollectionViewSource 则会成为数据源 ...
- WPF真入门教程12--ListView控件
ListView 控件在Windows应用程序中常用,用于表示数据列表.如果您以前使用过 WinForms,那么您对ListView的实用性有一个很好的了解,但您应该意识到 WPF中的ListView ...
- WPF真入门教程01--WPF简介
Windows Presentation Foundation (简称 WPF),WPF是微软推出的基于Windows 的用户界面框架,属于.NET Framework 3.0的一部分.它提供了统一的 ...
- WPF真入门教程21--WPF资源系统
WPF资源系统是一种保管一系列对象(如常用的画刷.样式或模版)的简单办法,从而使您更容易地复用这些对象. WPF允许在代码中以及在标记中的各个位置定义资源(如特定的控件或窗口或在整个应用程序中定义). ...
- WPF案例 (六) 动态切换UI布局
原文:WPF案例 (六) 动态切换UI布局 这个Wpf示例对同一个界面支持以ListView或者CardView的布局方式呈现界面,使用控件ItemsControl绑定数据源,使用DataTempla ...
最新文章
- java mybatis拦截配置_SpringMvc拦截器配置_JavaEE框架(Maven+SpringMvc+Spring+MyBatis)全程实战教程_Java视频-51CTO学院...
- 怎么高效刷LeetCode?
- 网页表格线框html,关于Dreamweaver中怎么让html网页中的table边框细线显示?
- 前端有关vue的面试题
- webview取java的参数值_Android 显示 WebView ,加载URL 时,向webview的 header 里面传递参数...
- Sharing A Powerful Tool For Calculate Code Lines
- tensorflow 显存 训练_tensorflow手动指定GPU以及显存大小
- 实践:VB创建Com组件 在Asp以及.Net中调用
- java html模板转图片、动态绑定数据
- Unity3D--学习太空射击游戏制作(二)
- 台式电脑显示配置100%请勿关闭计算机,Win7关机提示“配置Windows Update已完成30%请勿关闭计算机”怎么解决...
- 新能源智慧路灯充电桩可行性分析
- 因果分析系列2--随机试验
- Sublime text3 高级操作(直接上代码,超爽。)
- Android学习之zygote启动流程
- 免费/中文/功能强大的Modbus调试软件:MThings
- 12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- 大数据基础课第三课 spark_core、Spark_sqlSpark_streaming
- FANUC最新CNC系列0i-MODEL F
- 专业版OV通配符证书