WPF真入门教程05--UI布局2

article/2025/10/12 21:39:18

这一节,来学习下ComboBox下拉框,Border边框控件,Image图片控件,CheckBox复选框, 这些发挥着不同的作用,是布局基础组成部分,从左侧拖控件到右边,完成以下代码:

<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"/><!-- IsThreeState属性指定 ToggleButton 是否有两种或三种状态:true,false,null,即选中 、清除 和不确定--><CheckBox Content="汽车" Name="chkexcel" IsChecked="True" IsThreeState="True"  HorizontalAlignment="Left" Margin="45,138,0,0" VerticalAlignment="Top"/><CheckBox Content="阅读"   HorizontalAlignment="Left" Margin="118,138,0,0" VerticalAlignment="Top"/><CheckBox Content="唱歌" HorizontalAlignment="Left" Margin="181,138,0,0" VerticalAlignment="Top"/><CheckBox Content="体育" HorizontalAlignment="Left" Margin="241,138,0,0" VerticalAlignment="Top"/><Button Content="获取复选框" HorizontalAlignment="Left" Margin="301,135,0,0" VerticalAlignment="Top" Width="91" Click="Button_Click"/><Button Content="创建复选框" HorizontalAlignment="Left" Margin="409,134,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/><!--Image图片控件,Stretch指定拉伸模式 --><Image HorizontalAlignment="Left"  Name="myimg" Height="83" Margin="45,184,0,0" VerticalAlignment="Top" Width="85" Source="images/6p.jpg" Stretch="Fill" RenderTransformOrigin="0.541,0.157"/><Button Content="相对替换" HorizontalAlignment="Left" Margin="152,187,0,0" VerticalAlignment="Top" Width="69" Height="32" Click="Button_Click_2"/><Button Content="绝对替换" HorizontalAlignment="Left" Margin="152,240,0,0" VerticalAlignment="Top" Width="69" Height="28" RenderTransformOrigin="0.536,0.815" Click="Button_Click_3"/><!-- Border边框控件,与布局面板一起使用,里面只能有一个子元素,BorderThickness边框粗细,BorderBrush边框颜色,CornerRadius圆角大小,Background背景色--><Border BorderBrush="red" BorderThickness="3" HorizontalAlignment="Left" Height="32" Margin="266,184,0,0" VerticalAlignment="Top" Width="100"><Label Content="我有边框" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border><Border BorderBrush="BlueViolet" BorderThickness="5" CornerRadius="9" Background="Bisque" HorizontalAlignment="Left" Height="38" Margin="266,229,0,0" VerticalAlignment="Top" Width="100"><Label Content="我有圆角" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="16,1,16,3"/></Border><!--ComboBox下拉框,IsDropDownOpen指定是否展开,默认不展开 --><ComboBox HorizontalAlignment="Left" Name="hecbm" IsDropDownOpen="False" Margin="403,187,0,0" VerticalAlignment="Top" Width="81" Height="22"><ComboBoxItem Content="请选择" IsSelected="True" Background="#FF26D365"/><ComboBoxItem Content="亚洲"/><ComboBoxItem Content="非洲"/><ComboBoxItem Content="欧洲"/></ComboBox><ComboBox Name="mycbm" HorizontalAlignment="Left" Margin="403,241,0,0" VerticalAlignment="Top" Width="81" Height="27" RenderTransformOrigin="0.247,0.481"/></Grid>
</Window>

别看代码多,都是智能提示,方便很多。

第二步,在后台编写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);//动态指定下拉框数据源List<string> listinfo = new List<string>();listinfo.Add("请选择");listinfo.Add("南京");listinfo.Add("桂林");listinfo.Add("深圳");listinfo.Add("沈阳");mycbm.ItemsSource = listinfo;mycbm.SelectedIndex =2; }private void Button_Click(object sender, RoutedEventArgs e){//获取所有勾选的复选框的内容string txt = "";foreach(UIElement ele in mygrid.Children){if(ele is CheckBox){CheckBox chk = ele as CheckBox;if(chk.IsChecked==true){                       txt += chk.Content.ToString();txt += ",";}}}MessageBox.Show(txt.TrimEnd(','));//TrimEnd去掉最后一个字符}private void Button_Click_1(object sender, RoutedEventArgs e){string[] excel = { "足球", "兰球", "铁球" };for(int i=0;i<excel.Length;i++){CheckBox chkitem = new CheckBox();chkitem.Content = excel[i];chkitem.HorizontalAlignment = HorizontalAlignment.Left;chkitem.VerticalAlignment = VerticalAlignment.Top;chkitem.Margin = new Thickness(40 + i *60, 160, 0, 0);mygrid.Children.Add(chkitem);}}private void Button_Click_2(object sender, RoutedEventArgs e){//相对路径,UriKind.Relative路径类型myimg.Source = new BitmapImage(new Uri("/images/80zj.jpg",UriKind.Relative));}private void Button_Click_3(object sender, RoutedEventArgs e){//绝对路径,AppDomain.CurrentDomain.BaseDirectory表示当前程序的运行路径myimg.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory+"/imgs/3sk.jpg",UriKind.Absolute));}}
}

其中第2行复选框是后台动态添加的,第2个下拉框也是。代码中涉及C#基本语法,如列表List操作,所以需要有.Net基础才行。

第三步,运行程序,可以看到:

 

这一波操作又成功了,百尺杆头进了一小步,看来要加辣条和啤酒了。


http://chatgpt.dhexx.cn/article/m2ItkIge.shtml

相关文章

WPF基本介绍

WPF基本介绍 WPF&#xff08;Windows Presentation Foundation&#xff09;是&#xff08;微软推出的&#xff09;基于Windows的用户界面框架&#xff0c;属于.NET Framework 3.0的一部分。WPF提供了统一的编程模型&#xff0c;语言和框架&#xff0c;做到了分离界面设计人员与…

WPF UI介面的革新

透過 Microsoft 對 Windows Vista 及 WPF 的強力行 銷&#xff0c;相信許多讀者對於WPF的UI能力只有驚豔二字可以形容&#xff0c;對於如何使用WPF來達到這些效果&#xff0c;基於雜誌與網路上已有相當多的文章討論&#xff0c;筆者於此就不再重述了&#xff0c;直接將主軸放在…

WPF界面设计—撸大师

WPF界面设计,模仿了金山卫士,360,鲁大师的界面! <!--无边框窗体--><Style x:Key="NoResize_window" TargetType="{x:Type Window}"> <Setter Property="AllowsTransparency" Value="true"/> <Setter Prope…

WPF - 简单的UI框架

实现了一个简单的WPF应用程序UI框架 &#xff0c;分享出来。界面效果图如下&#xff1a; 运行效果如下&#xff1a; 打算持续更新&#xff0c;将左侧面板所有功能模块全给实现了。 喜欢的可以下载源码体验&#xff1a;https://github.com/DuelWithSelf/WPFEffects 左侧分类导览…

WPF真入门教程04--UI布局1

大家都知道&#xff1a;UI是做好一个软件很重要的因素&#xff0c;如果没有一个漂亮的UI&#xff0c;功能做的再好也无法吸引很多用户使用&#xff0c;而且没有漂亮的界面&#xff0c;那么普通用户会感觉这个软件没有多少使用价值。 WPF系统基于流布局的标准&#xff0c;开发人…

WPF UI界面控件篇

WPF UI界面控件篇 布局控件&#xff1a;是任何用户界面的基础&#xff0c;排列应用中的 UI 元素。 文本、按钮和图像等元素都需要规定自己位置和行为方式&#xff0c;构建基块称为“控件”&#xff0c;有时亦称为“元素”。 <Window x:Class"UsingLayoutsApp.Wpf.Mai…

WPF(一) WPF基本控件与布局

​ WPF&#xff08;Windows Presentation Foundation&#xff09;是微软推出的基于Windows的用户界面框架&#xff0c;中文译为“Windows呈现基础”&#xff0c;属于.NET Framework 3.0的一部分。WPF类似于WinForm技术框架&#xff0c;但是相比于WinForm&#xff0c;WPF对大部分…

WPF炫酷界面设计

一.效果展示&#xff08;多层次&#xff09; 二.制作流程 1.在vs2012中建立一个wpf程序 2.建立一个主页面&#xff08;.cs&#xff09;(注&#xff1a;C#程序每一个页面都由两个文件构成一个axml一个cs&#xff0c;一个前端文件一个后台文件) 3.在主页面中添加按钮&#xff0c;…

WPF界面设计工具---Blend学习(一)

文章目录 前言一.初用Blend之喜二.使用Blend1.Blend工具版本 前言 最近在空闲时间学习WPF界面设计工具Blend&#xff0c;之前写WPF的界面都是完全依靠手敲代码的方式&#xff0c;这种方式往往很低效率而且很难做到一些复杂的效果。比如动画&#xff0c;手敲代码实现动画的话&a…

C# WPF 一个设计界面

微信公众号&#xff1a;Dotnet9&#xff0c;网站&#xff1a;Dotnet9&#xff0c;问题或建议&#xff1a;请网站留言&#xff0c; 如果对您有所帮助&#xff1a;欢迎赞赏。 C# WPF 一个设计界面 今天正月初三&#xff0c;大家在家呆着挺好&#xff0c;不要忘了自我充电。 武汉…

WPF界面设计

目录 1.设计一个优美的注册界面1.实现效果2.代码展示 2.简易登录按钮设计1.实现效果2.代码展示 3.设计一个优美的注册登录界面&#xff08;连接数据库&#xff09;1.实现效果2.代码展示 4.设计一个简单的在线教育系统界面1.实现效果2.代码展示 5. 设计一个Dashboard1.实现效果2…

WPF实例系列一:登录、注册界面设计

WPF实例系列一&#xff1a;登录、注册界面设计 文章目录 WPF实例系列一&#xff1a;登录、注册界面设计前言一、实例演示1. 登录界面展示2. 注册界面展示3. 数据存储4. 效果演示 二、结构及源码1.主界面跳转登录界面设计2.登录界面设计3.注册界面设计4.Excel保存数据类设计5.源…

C# WPF界面设计参考 工控机上位机界面 美观炫酷的现代化风格界面设计实际案例分享 界面设计代码

1.动图效果展示 2.界面1 3.界面2 4.界面3 需要界面效果代码的可留言联系博主

WPF界面设计学习

github上发现了一个不错的项目&#xff1a; https://github.com/HenJigg/wpf-uidesign 还配有B站的学习视频&#xff1a; https://space.bilibili.com/32497462 看了这个项目&#xff0c;觉得自己学了这么多年的WPF&#xff0c;界面还设计的这么丑&#xff0c;顿时脸都不知道往…

C# WPF十个美观的界面设计展示

概述 很多时候&#xff0c;我们设计的界面总是感觉缺乏美感&#xff0c;不是我们不会开发好看的界面&#xff0c;而是不知道怎么才算美观&#xff0c;这时候我们不妨看看别人好的页面是怎么做的.下面展示一些我觉得做的比较好的cs界面&#xff0c;希望能给大家在平时做界面设计…

WPF|分享一个登录界面设计

分享一个登录界面&#xff0c;先看效果图&#xff1a; 准备 文中使用到了一些图标&#xff1a; [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZsHcZCFv-1652400544809)(https://img1.dotnet9.com/2022/05/3402.png)] 我们可以从 iconfont免费下载…

行列式的意义

本文转自http://www.cnblogs.com/AndyJee/p/3491487.html 三、行列式的几何意义&#xff1a; 行列式的定义&#xff1a; 行列式是由一些数据排列成的方阵经过规定的计算方法而得到的一个数。当然&#xff0c;如果行列式中含有未知数&#xff0c;那么行列式就是一个多项式。它…

矩阵——对称行列式快解

1、先化成爪型行列式 2、再化成上三角或下三角 第一步&#xff1a;把第1行的1倍分别加至第2、3、4行&#xff0c;化为爪型行列式 第二步&#xff1a;把第2、3、4列的&#xff08;-1&#xff09;倍都加到第1列&#xff0c;化为上三角 第三步&#xff1a;得出结果

【线性代数】一、行列式和矩阵

一、行列式 文章目录 一、行列式1.1 行列式性质1.2 余子式行列式按照行列展开的展开公式 一、行列式求解1.用行列式2.用矩阵3.用特征值 1.3 行列式计算一、具体形行列式(1)直接运算1.行\列和相等类型2.爪形、异爪形行列式 (2)化为121个基本行列式1. 主副对角线行列式2. 拉普拉斯…

矩阵/行列式的意义

一、代数意义 矩阵乘法规则看起来比较复杂&#xff0c;不容易理解其乘法规则背后隐含的意义。现举一个例子说明矩阵乘法的意义。如下图所示&#xff0c;一个商店出售Beef pie&#xff0c;chicken pie&#xff0c;vegetable pie&#xff0c;其单价分别为3元&#xff0c;4元&…