xmal随笔

WPF

WPF 全称 Windows Presentation Foundation WPF 又叫 Windows呈现基础

Windows用户界面框架, .net framework3.0一部分,同一的编程模型,语言框架,**做到了页面设计与后端开发分离! **多媒体支持,制作动画.

特点:

1. .net framework 3.0及以上版本
2. 不受分辨率的影响,自动适应
3. DirectX 3D 3D界面 做出更炫酷的界面
4. .net UI框架,集成了矢量图,流动文字支持3d视觉效果和控件模型框架
5. ui 与 业务逻辑彻底分离, ui-xaml描述, WPF引擎解释为.net对象
6. 数据驱动ui 数据是核心 winform事件驱动

控件分类

布局控件 Panel
内容控件 ContentControl 只能容纳一个控件或布局控件
带标题内容控件 内容控件可以设置标题 Header 父类:HeaderedContentControl
条目控件 可以显示一列数据,数据类型一般相同 父类:ItemControl
带标题的条目控件 条目控件可以设置标题 Header 父类: HeaderedItemControl
特殊内容控件 常用的控件: TextBox PasswordBox TextBlock Image等

XAML介绍

1.定义:为构建应用程序用户页面而创建的一种新的 "可扩展的应用程序标记语言",提供一种便于扩展和定位的语法来定义和程序业务逻辑分离的用户页面

2.特点:

定义应用程序的页面元素
显示声明WPF资源(样式,模板,动画等)
可扩展性(ui控件)

命名空间

xaml和.net程序一样,也是通过命名空间有效组织xaml内部的相关元素类

命名空间像网址(打不开),规则(xaml解释器标准);

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  默认命名空间
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  语法和编译相关的clr命名空间
区分:如果应用是不带前缀,就来着默认的命名空间,带前缀的就是来着带前缀的命名空间

引用命名空间:xmlns:前缀="命名空间描述"

自定义类或程序集映射语法

xmlns:前缀="clr-namespace:命名空间;assembly=程序集名称"

WPF应用基本结构组成

App.config // 配置文件 配置信息...
App.xaml // 应用程序起始文件,系统级资源
App.xaml.cs // app.xaml 文件后台类文件
MainWindow.xaml // WPF界面与xaml设计文件
MainWindow.xaml.cs // MainWindow.xaml 后台代码文件

App.xaml

StartupUri="MainWindow.xaml" // 指定起始文件是谁
<Application.Resources>
     
</Application.Resources> // 定义整个wpf应用程序的相关资源

window

window--contentControl  					//内容控件--只能承载一个content ---Grid

ShowInTaskbar="True" 						//窗口是否在任务栏显示

WindowStartupLocation="CenterScreen" 		//显示窗口初始化显示位置

WindowState="Maximized" 					//设置窗口是否是最大化最小化

Topmost="True" 								//设置窗口是否是在最上方

Icon="../image/Imageres_dll_101.ico" 		//设置窗口图标

Loaded="Window_Loaded"						//窗口加载事件
    
Closing="re"								//窗口关闭事件...

<Window x:Class="WpfApp2.User.Window1"
        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:WpfApp2.User"
        mc:Ignorable="d"
        Title="测试操作" Height="623.338" Width="807.639" ShowInTaskbar="True" WindowStartupLocation="CenterScreen"
        WindowState="Normal"
        Topmost="True"
        Icon="../image/Imageres_dll_101.ico"
        Loaded="Window_Loaded" 
        Closing="re"
        >
    <Grid>
        <Button Content="登录" HorizontalAlignment="Left" Margin="220,164,0,0" VerticalAlignment="Top" Width="120" Height="23"/>
        <Label Content="账号:" HorizontalAlignment="Left" Margin="178,92,0,0" VerticalAlignment="Top"/>
        <Label Content="密码:" HorizontalAlignment="Left" Margin="178,126,0,0" VerticalAlignment="Top"/>
        <TextBox Name="txtUName" HorizontalAlignment="Left" Height="23" Margin="220,94,0,0" TextWrapping="NoWrap"  Text="admin" VerticalAlignment="Top" Width="120" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
        <PasswordBox Name="txtpsssword" HorizontalAlignment="Left" Margin="220,128,0,0" VerticalAlignment="Top" Width="120" Height="23" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
    </Grid>
</Window>

Label 文本标签 ContentControl

TextBox 文本框 信息编辑 特殊内容控件

PasswordBox

Button 按钮 ContentControl

HorizontalAlignment="Left" 					//水平方式居中
VerticalAlignment="Top"						//垂直方式居中
Margin="220,164,0,0"						//与元素上下左右的间距
BorderBrush="Red" 							//边框颜色
BorderThickness="3" 						//边框粗细
Background="Yellow"							//背景颜色
Foreground="Yellow" 						//字体颜色
ImageSource=""								//图像源
<Button Content="登录" HorizontalAlignment="Left" Margin="220,164,0,0" VerticalAlignment="Top" Width="120" Height="40" BorderBrush="Red" BorderThickness="3">
    <Button.Background>
        <ImageBrush ImageSource="../image/Imageres_dll_101.ico"></ImageBrush>
    </Button.Background>
</Button>

RadioButton

分组

<RadioButton Content="RadioButton" GroupName="group1" HorizontalAlignment="Left" Margin="429,126,0,0" VerticalAlignment="Top"/>
<RadioButton Content="RadioButton" GroupName="group1" HorizontalAlignment="Left" Margin="429,153,0,0" VerticalAlignment="Top"/>
GroupName="group1"							//分组
IsChecked="True"							//默认选中

CheckBox

复选框,允许可以选择多个 父类:toggleButton

IsChecked="True"                 // 选中
IsThreeState="True"				 // 三种状态

获取已勾选选项 父容器--Grid Children ---子元素的集合

private void Button_Click(object sender, RoutedEventArgs e)
        {
            foreach (UIElement item in grid1.Children)
            {
                // 判读是否是CheckBox对象
                if (item is CheckBox)
                {
                    // 如果是的话转换 检查对象类型的兼容性,并返回转换结果,如果不兼容则返回null;
                    CheckBox bo = item as CheckBox;
                    if (bo.IsChecked == true)
                    {
                        MessageBox.Show(bo.Content.ToString());   
                    }

                }
            }
        }

Image控件

Stretch="Fill" 							// 图片的填充模式 默认
StretchDirection="DownOnly"   			// 拉伸方向
Source="image/123.jpg"					// 图片来源 相对路径

代码指定图像源

imgPic.Source = new BitmapImage(new Uri("image/微信图片_20210622135043.jpg", UriKind.Relative));

SourceUpdated事件

Border控件

可以用于其他元素的边框背景色等

BorderBrush="Black"  					// 边框颜色
BorderThickness="3" 					// 边框大小
CornerRadius="15"						// 边框圆角
吾虽浪迹,却未迷失本心
原文地址:https://www.cnblogs.com/lddragon1/p/15538318.html