WPF基础知识

WPF布局基础原则

复制代码
1.一个窗口中只能包含一个元素
2. 不应该显示设置元素尺寸
3. 不应使用坐标设置元素的位置
4.可以嵌套布局容器

WPF布局容器

  • StackPanel:水平或垂直排列元素,Orientation属性分别为:Horizontal/Vertical
  • WrapPanel:水平或垂直排列元素、针对剩余空间不足会进行换行或换列进行排列。
  • DockPanel:根据容器的边界、元素进行Dock.Top(Left、Right、Bottom)设置 默认情况最后一个元素会填充满 LastChildFill="False"
  • Grid:类似Table表格,可以灵活设置行列并且放置控件元素(最常用的)
  • UniformGrid:指定行和列的数量,均分有限的容器空间。
  • Canvas:使用固定的坐标设置元素的位置,不具备停靠等功能。

WPF样式:TargetType:样式所指定的类型

Foreground:字体颜色

FontSize:字体大小

FontFamily:字体

Background:背景颜色

margin边距

CornerRadius:圆角矩形

HorizontalAlignment:水平位置

VerticalAlignment:垂直位置
触发器的使用

1:使用场景:例如当鼠标碰到按钮时候字号为30,字体颜色为红色,当鼠标为碰到按钮时候字号为20 ,字体颜色为蓝色;

csharp 复制代码
  <Style x:Key="MM" TargetType="Button">
            <Setter Property="Width" Value="300"/>
            <Setter Property="Height" Value="48"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="FontSize" Value="30"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Foreground" Value="Blue"/>
                    <Setter Property="FontSize" Value="20"/>
                </Trigger>
            </Style.Triggers>
        </Style>

多条件触发器的使用

csharp 复制代码
<Style x:Key="MM" TargetType="Button">
            <Setter Property="Width" Value="300"/>
            <Setter Property="Height" Value="48"/>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsMouseOver" Value="True"/>
                        <Condition Property="IsFocused" Value="True"/>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="Foreground" Value="Red"/>
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>

事件触发器

自定义控件模板

Cell Template数据模板

引用样式的方法:

StaticResource:静态的

DynamicResource动态的

相关推荐
软泡芙21 小时前
【WPF 】MVVM 设计模式在 WPF 中的实战应用
设计模式·wpf
张小俊_1 天前
WPF 跨线程 UI 更新与硬编码赋值引发的 Bug 排查
c#·bug·wpf
七夜zippoe2 天前
DolphinDB在工业物联网中的优势
物联网·wpf·工业物联网·优势·dolphindb
heimeiyingwang2 天前
【架构实战】观察者模式在分布式系统中的应用
观察者模式·架构·wpf
bugcome_com2 天前
WPF + Microsoft.ToolKit.Mvvm 技术指南与实战项目
microsoft·wpf
武藤一雄3 天前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
炸炸鱼.3 天前
ELK 企业级日志分析系统完整部署手册
elk·wpf
Mr_pyx4 天前
微服务可观测性实战:分布式链路追踪从入门到精通
wpf
c#上位机5 天前
wpf附加事件
wpf
玖笙&5 天前
✨WPF编程进阶【9.1】:WPF资源完全指南(附源码)
c++·c#·wpf·visual studio