Border控件(边框布局) - WPF中文网 - 从小白到大佬

nuget包

1.MetroWindow

轻量级应用程序快速“美容”的利器,是 WPF 中常用的窗口类 Window 的一个增强版本,提供了现代化的 UI 风格

  • 1-安装 MahApps.Metro,2-在app.xaml中添加

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
    <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <!-- Theme setting -->
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

2.MaterialDesignColors、MaterialDesignThemes、MaterialDesignThemes.MahApps

MaterialDesignColors提供了许多官方的 Material Design 颜色集合,可以让你轻松地在 WPF 中使用这些颜色。这些颜色集合包括了标准的颜色,如 PrimarySecondaryAccent 等,以及多种深浅变体。

  • 1-安装MaterialDesignColors 库 2-定义一个主题色

<SolidColorBrush x:Key="PrimaryColor" Color="{DynamicResource Primary500}" />

MaterialDesignThemes 是一个主要提供 Material Design 样式、控件和主题的库

  • 1-安装MaterialDesignThemes 库 2-在app.xaml中添加样式资源

<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />

MaterialDesignThemes.MahApps 提供了将 MahApps.Metro 控件(如 MetroWindow)与 Material Design 风格结合使用的能力。

  • 1-安装 MaterialDesignThemes.MahApps 库 2-在app.xaml中添加资源

<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign3.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.MahApps;component/Themes/MaterialDesignTheme.MahApps.Flyout.xaml" />
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"

3.mahApps.Metro.IcoPacks、MahApps.Metro.IconPacks.Material

主要用于在 WPF 应用程序中使用丰富的图标。

https://pictogrammers.com/library/mdi/

4.PropertyChanged.Fody

自动为属性添加 INotifyPropertyChanged 的实现,减少了手动写通知代码的工作量。

  • 1-安装PropertyChanged.Fody包 2-添加[AddINotifyPropertyChangedInterface] WPF 的数据绑定需要依赖 INotifyPropertyChanged 来通知 UI 更新。当你修改了 开始时间结束时间 的值时,如果没有通知机制,绑定的 UI 无法感知属性值的变化,因此显示的是默认值(DateTime.MinValue)。 加上这个特性后,属性在被赋值时会自动触发 PropertyChanged 事件,通知 UI 更新绑定的值,所以能够显示正确的时间。

5.Notifications.Wpf.Core

提供了一种简便的方式来在桌面应用程序中显示提醒、警告、成功或错误消息。

  • 1-安装Notifications.Wpf.Core库 2-添加如下代码

<notifications:NotificationArea  
     MaxItems="3" x:Name="WindowArea"
     Position="TopRight" Grid.ColumnSpan="10" Grid.RowSpan="10" />
NotificationManager notificationManager = new NotificationManager(NotificationPosition.TopRight);
_ = notificationManager.ShowAsync(new NotificationContent
{
    Title = "连接成功",
    Message = "连接成功!",
    Type = NotificationType.Success
}, areaName: "WindowArea");
​
_ =:这是 C# 7.0 中引入的“弃用变量”操作符,表示不关心异步方法的返回值,直接忽略它。通常,如果你不需要返回值,可以使用 _。

双向绑定

DataContext="{Binding RelativeSource={RelativeSource Self}}">
​
​
<d:MetroWindow.DataContext>     ——binding 直接绑定构造函数里的属性 
    <local:主界面/>             ——注意<local:主页面>自己手打
</d:MetroWindow.DataContext>
    DataContext = this;

Canvas 适合绝对定位,StackPanel 和 WrapPanel 用于简单的垂直/水平布局,Grid 适合复杂的表格布局,DockPanel 适用于固定停靠的布局,UniformGrid 用于均匀网格布局,ViewBox 支持内容的缩放,而 VirtualizingStackPanel 用于处理大量数据时的虚拟化。选择合适的布局容器可以让你的 UI 更加高效和灵活。

Grid.RowGrid.Column 来定位 GroupBox 控件在 Grid 布局中的位置。

RichTextBox 允许文本内容包含不同的格式,如字体、颜色、大小、对齐方式等。

BorderThickness="0":去除边框,使其更加简洁。

Background="Transparent":设置背景为透明,使其看起来像是无背景。

FlowDocument 可以包含不同类型的元素,如 Paragraph(段落)、List(列表)、Table(表格)、Image(图片)等,通过 ParagraphInlines 属性来动态更新文本内容。通过 Run 元素插入新的文本

DataGridTextColumn Header="信号名称"

Binding="{Binding 信号名}" 表示这一列会绑定数据源中的 信号名 属性。如果数据源中的对象有一个名为 信号名 的属性,那么该列就会显示这些数据。

<DataGridTextColumn Header="更新时间" Binding="{Binding 更新时间,StringFormat=\{0:yyyy-MM-dd HH:mm:ss\}}"/>
public BindingList<表格显示> 信号表格 { get; set; } = new(); 
[AddINotifyPropertyChangedInterface]
public partial class 表格显示
{
    public string 信号名 { get; set; }
    public string 值 { get; set; }
    public DateTime 更新时间 { get; set; }
}