C# WPF布局

布局:

1、Grid:

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid Margin="0,0,12,0">

     <!--布局容器-->

        <Grid.RowDefinitions>

     <!--定义它的行以及它的高度-->

            <RowDefinition Height="40"></RowDefinition>

            <RowDefinition Height="Auto"></RowDefinition>

            <RowDefinition Height="2*"></RowDefinition>

            <RowDefinition Height="*"></RowDefinition>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

     <!--定义它的列以及它的宽度-->

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

            <ColumnDefinition Width="200"></ColumnDefinition>

        </Grid.ColumnDefinitions>

   

        <Button Grid.Row="0" Grid.Column="2" Content="button1"></Button>

     <!--第0行第二列-->

        <Button Grid.Row="0" Grid.Column="1" Content="button3"></Button>

     <!--第0行第1列-->

        <Button Grid.Row="1" Content="button2"></Button>

     <!--第一行-->

        <Button Grid.Row="2" Content="button4"></Button>

     <!--//第二行-->

        <Button Grid.Row="3" Content="button5"></Button>

     <!--//第三行-->

    </Grid>

</Window>

StackPanel:按行按列排序

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

   2、 <Grid>

        <StackPanel  Name="Stcak1" Orientation="Horizontal">

            <Button Content="button1"/>

            <Button Content="button2"/>

        </StackPanel>

        <StackPanel x:Name="Stack2" Orientation="Vertical">

            <Button Content="button3"></Button>

            <Button Content="button4"></Button>

            <Button Content="button5"></Button>

        </StackPanel>

        <StackPanel Name="stack3" Orientation="Horizontal" FlowDirection="RightToLeft">

            <Button Content="button6"></Button>

            <Button Content="button7"></Button>

        </StackPanel>

    </Grid>

3、WrapPanel://自动换行环列

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <WrapPanel Orientation="Horizontal">

        <Button Content="button 150" Width="150"></Button>

        <Button Content="button 200" Width="200"></Button>

        <Button Content="button 150" Width="150"></Button>

        <Button Content="button 200" Width="200"></Button>

        <Button Content="button 150" Width="150"></Button>

    </WrapPanel>

</Window>

DockPanel:

    <DockPanel>

        <Button Content="左"  DockPanel.Dock="Left"></Button>

        <Button Content="下"  DockPanel.Dock="Bottom" ></Button>

        <Button Content="右"  DockPanel.Dock="Right"></Button>

        <Button Content="上"  DockPanel.Dock="Top" ></Button>

    </DockPanel>

4、UniformGrid://按照输入顺序排列到容器当中

    <UniformGrid >

        <Button Content="Button"></Button>

        <Button Content="Button1"></Button>

        <Button Content="Button2"></Button>

        <Button Content="Button3"></Button>

        <Button Content="Button4"></Button>

    </UniformGrid>

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Canvas>

            <Button   Content="Button1" Canvas.Left="50"  Canvas.Top="50"></Button>

            <Button   Content="Button2" Canvas.Right="50" Canvas.Top="50" ></Button>

            <Button   Content="Button3" Canvas.Left="50"  Canvas.Bottom="50" ></Button>

            <Button   Content="Button3" Canvas.Left="50"  Canvas.Bottom="50" ></Button>

        </Canvas>

    </Grid>

</Window>

ScrollViewer:滑动框

    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">

        <Button Content="Button" Width="800" Height="800"></Button>

    </ScrollViewer>

ViewBox:

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>

        <Viewbox Grid.Row="0" Grid.Column="0" Stretch="None">

            <Button Width="100" Height="50" Content="None"></Button>

        </Viewbox>

        <Viewbox Grid.Row="0" Grid.Column="1" Stretch="Uniform">

            <Button Width="100" Height="50" Content="Uniform"></Button>

        </Viewbox>

    </Grid>

样式:

内部样式:

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>

        <Style TargetType="Button">/设置样式的类型,全局样式

            <Setter Property="Background" Value="WhiteSmoke"></Setter>//设置背景属性的样式

            <Setter Property="FontSize"  Value="20"></Setter>//设置文本字体的样式

            <Setter Property="Margin"  Value="10, 20"></Setter>//设置边框的外部样式

        </Style>

        <Style x:Key="loginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">//绑定单个样式

            <Setter Property="Background" Value="CadetBlue"></Setter>

        </Style>

    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>

        <Button Style="{StaticResource loginStyle}" Content="Button1" Grid.Column="0" Grid.Row="1" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="2" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="0" Width="Auto" Height="Auto" ></Button>

    </Grid>

</Window> 

外部样式:

首先创建一个xaml文件

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

   

        <Style TargetType="Button">

            <Setter Property="Background" Value="WhiteSmoke"></Setter>

            <Setter Property="FontSize"  Value="20"></Setter>

            <Setter Property="Margin"  Value="10, 20"></Setter>

        </Style>

        <Style x:Key="loginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">

            <Setter Property="Background" Value="CadetBlue"></Setter>

        </Style>

</ResourceDictionary>

然后在App.xaml种添加

引用路径

<Application x:Class="WpfApp2.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:local="clr-namespace:WpfApp2"

             StartupUri="MainWindow.xaml">

    <Application.Resources>

        <ResourceDictionary>

            <ResourceDictionary.MergedDictionaries>

                <ResourceDictionary Source="/WpfApp2;component/Dictionary1.xaml"/>

            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>

    </Application.Resources>

</Application>

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

            <ColumnDefinition></ColumnDefinition>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

            <RowDefinition></RowDefinition>

        </Grid.RowDefinitions>

        <Button Style="{StaticResource loginStyle}" Content="Button1" Grid.Column="0" Grid.Row="1" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="2" Width="Auto" Height="Auto" ></Button>

        <Button Content="Button1" Grid.Column="0" Grid.Row="0" Width="Auto" Height="Auto" ></Button>

    </Grid>

</Window>

自定义样式模板及触发器

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Button Content="自定义按钮"  Height="100" Width="200" Background="#0078d4" FontSize="50" Foreground="WhiteSmoke" BorderBrush="Aqua" BorderThickness="1">

            <Button.Template>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">

                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>

                    </Border>

                </ControlTemplate>  

            </Button.Template>

        </Button>

    </Grid>

</Window>

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Button Content="自定义按钮"  Height="100" Width="200" Background="#0078d4" FontSize="50" Foreground="WhiteSmoke" BorderBrush="Aqua" BorderThickness="1">

            <Button.Template>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">

             

                        <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>

                    </Border>

                </ControlTemplate>  

            </Button.Template>

        </Button>

    </Grid>

</Window>

触发器绑定:

<Window x:Class="WpfApp2.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:WpfApp2"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Button Content="自定义按钮"  Height="100" Width="200" Background="#0078d4" FontSize="50" Foreground="WhiteSmoke" BorderBrush="Aqua" BorderThickness="1">

            <Button.Template>

                <ControlTemplate TargetType="{x:Type Button}">

                    <Border x:Name="boder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="20">

             

                        <TextBlock x:Name="txt" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>

                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="true">//绑定鼠标移动

                            <Setter TargetName="boder" Property="Background" Value="Blue"/>

                         <Setter TargetName="txt" Property="FontSize" Value="20"/>

                        </Trigger>

                        <Trigger Property="IsPressed" Value="true">//绑定鼠标点下去的

                            <Setter TargetName="txt" Property="Background" Value="red"/>

                         <Setter TargetName="txt" Property="FontSize" Value="20"/>

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>  

            </Button.Template>

        </Button>

    </Grid>

</Window>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/563407.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SpringBoot---------Lombook

Lombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具&#xff0c;通过使用对应的注解&#xff0c;可以在编译源码的时候生成对应的方法&#xff0c;也就是简化咱们之前pojo&#xff0c;实体类里面臃肿的get/set有参无参。 首先查看一…

LiveNVR监控流媒体Onvif/RTSP常见问题-如何对比监控摄像头延时视频流延时支持webrtc视频流播放超低延时播放

LiveNVR如何对比监控摄像头延时视频流延时支持webrtc视频流播放超低延时播放 1、问题场景2、如何对比延时&#xff1f;3、WEBRTC延时对比4、LiveNVR支持WEBRTC输出5、RTSP/HLS/FLV/RTMP拉流Onvif流媒体服务 1、问题场景 需要低延时的视频流监控播放&#xff0c;之前可以用rtmp…

react合成事件与原生事件区别备忘

朋友问起在做一个下拉框组件&#xff0c;下拉的点击事件是用react的onClick触发&#xff0c;外部区域点击关闭则用dom的原生点击事件绑定&#xff0c;问题是下拉的点击事件无法阻止冒泡到dom的原生事件。 我说&#xff0c;react的合成事件 和 原生事件是不一样的&#xff0c;尽…

前端表单input的简单使用

1.代码结构介绍 2.实战效果

【嵌入式linux】Ubuntu 修改用户名

第一次打开Ubuntu时不小心把初始用户名“siriusiot”写成“siriousiot”&#xff08;多了一个o&#xff09; 。作为技术人&#xff0c;我们要保持严谨&#xff0c;我们要纠正过来&#xff08;其实就是单词拼错了怕被笑话&#xff09;。 打开终端&#xff0c;输入&#xff1a; …

TypeError: Cannot read property ‘forceUpdate‘ of undefined

今天给大家展示一个 我自己在写项目的时候遇到的保存 其实很简单就是没有修改addid 把自己的小程序appid填上去就好了 学习记录笔记&#xff01;

【高校科研前沿】东北地理所孙敬轩博士为一作在《中国科学:地球科学(中英文版)》发文:气候变化下东北地区农业绿水安全风险评估

目录 01 文章简介 02 研究内容 03 文章引用 04 期刊简介 01 文章简介 论文名称&#xff1a;Risk assessment of agricultural green water security in Northeast China under climate change&#xff08;气候变化下东北地区农业绿水安全风险评估&#xff09; 第一作者及…

实验 3--表的基本操作与数据查询

文章目录 实验 3--表的基本操作与数据查询4.3.1 实验目的4.3.2 实验准备实验内容1.在 SSMS 中向数据库 YGKQ 中的表插入数据。2.使用 T-SQL 语句向 YGKQ 中的表插入数据。3.在 SSMS 中删除数据库 YGKQ 中的表数据。4.使用 T-SQL 语句删除数据库 YGKQ中的表数据。5.在 SSMS 中修…

ChatGPT基础(三) 让ChatGPT回答质量提高十倍的提示词模版

上篇文章介绍了ChatGPT使用提示词的一些方法策略和如何优化我们的提示词。这里呢&#xff0c;我介绍一下参照大佬的方法总结的一个提示词的一个用法的模板。使用这个模板之后&#xff0c;我们的提问和获得答案的效率和收集素材的完整度能提高很多。 首先我介绍一下这个模板&am…

NUMA测试

一、开启NUMA 添加链接描述 二、绑定核数 nerdctl update --cpuset-cpus0-7 3aecd121924a enable_thread_pool true thread_pool_attr 512, 2, (allbind)

day48_servlet

今日内容 周一 0 复习上周 1 本周计划 2 MVC和三层架构 3 Login案例 4 请求转发 5 重定向 0 复习昨日 1 jdbc五大步骤 注册驱动(反射)获得连接获得执行sql对象执行SQL关流 2 什么是SQL注入 通过SQL关键词,在执行SQL时出现不正常的情况 3 PreparedStatement怎么使用,有什么特点 …

SpringAOP从入门到源码分析大全(四)SpringAOP的源码分析

文章目录 系列文档索引六、EnableAspectJAutoProxy源码分析1、AnnotationAwareAspectJAutoProxyCreator源码&#xff08;1&#xff09;wrapIfNecessary方法&#xff08;2&#xff09;createProxy 2、getAdvicesAndAdvisorsForBean查找所有Advisor&#xff08;1&#xff09;find…

人工智能入门(一):基于Pytorch的手写数字识别模型

前言&#xff1a; 因为还在上学&#xff0c;时间不太够用&#xff0c;很多内容写到后面心有余力不足&#xff0c;未来有时间我会慢慢补充。人工智能的知识涉猎范围广又杂乱无章&#xff0c;啃书或上课学到的知识往往很早就过时了或者离实际的项目无关。所以&#xff0c;我很希…

安装mmsegmentation默认主分支main

安装时间2024.4.21 mmsegmentation新版本main分支&#xff08;v1.2.2&#xff09; 安装过程 conda create --name openmmlab python3.8 -y conda activate openmmlab// 很关键&#xff0c;可以避免mmcv版本问题 pip install torch1.10.1cu113 torchvision0.11.2cu113 torcha…

【力扣 Hot100 | 第七天】4.22(移动零)

文章目录 1.移动零1.1题目1.2解法&#xff1a;双指针1.2.1双指针思路1.2.2代码实现 1.移动零 1.1题目 给定一个数组 nums&#xff0c;编写一个函数将所有 0 移动到数组的末尾&#xff0c;同时保持非零元素的相对顺序。 请注意 &#xff0c;必须在不复制数组的情况下原地对数…

[docker] volume 补充 环境变量 参数

[docker] volume 补充 & 环境变量 & 参数 这里补充一下 volume 剩下的内容&#xff0c;以及添加参数(ARG) 和 环境变量 ENV 的内容 read only volumes ❯ docker run-p 3000:80--rm--name feedback-app-v feedback:/app/feedback-v "$(pwd):/app"-v /app/…

第 394 场 LeetCode 周赛题解

A 统计特殊字母的数量 I 哈希&#xff1a;遍历然后枚举 class Solution {public:int numberOfSpecialChars(string word) {unordered_map<char, int> m;for (auto ch : word)m[ch] 1;int res 0;for (char ch a; ch < z; ch)if (m.count(ch) && m.count(A …

爬虫采集:数据提取

目录 1. 数据分类 2. JSON 2.1 json数据转换​编辑 3. 正则表达式 3.1 re模块 3.1.1 常见方法 3.1.2 单字符匹配 3.1.4 匹配开头和结尾 3.1.5 分组匹配 3.1.6 贪婪非贪婪匹配 4. Xpath 4.1 语法 4.2 查找特定节点 4.3 lxml 模块 4.3.1 安装 4.3.2 导入 4.3.3 使…

【行为型模式】命令模式

一、命令模式概述 命令模式的定义&#xff1a;将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。(对象行为型) 命令模式优缺点&#xff1a; 优点&#xff1a; 1.类间解耦&#xff1a;调用者角色与接收者角色之间没有任何依…

TPG原理以及verilog实现

文章目录 一、前言二、verilog代码实现三、仿真以及结果分析 一、前言 TPG(video_test_pattern generator) 视频测试模式发生器用于产生测试数据&#xff0c;对视频数据通路测试。根据视频输出时序产生相应的图像数据 二、verilog代码实现 timescale 1ns / 1nsmodule tpg ( i…