WPF【11_3】WPF实战-重构与美化(可复用的UI组件)

11-7 【UI】美化主页面

04:40

既然要使用到图片,肯定要把图片设为静态资源保存到项目中。图片复制到VS项目中后,右击图片 - Properties,找到 Build Action: Resource ,这个图片就可以被WPF程序打包了。

UI重新布局:

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto"/>

<RowDefinition />

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="240"/>

<ColumnDefinition Width="280"/>

<ColumnDefinition />

</Grid.ColumnDefinitions>

<!--header-->

<Border Grid.ColumnSpan="3" Background="#7f3089">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">

<Image Height="90" Margin="5" Source="/Images/logo.jpg"/>

<TextBlock Text="WPF客户管理系统" FontSize="40" VerticalAlignment="Center" Foreground="#ffffff"/>

</StackPanel>

</Border>

<StackPanel Grid.Row="1" Grid.Column="0">

<Button Content="添加客户"/>

<ListView />

</StackPanel>

<StackPanel Grid.Row="1" Grid.Column="1">

<TextBlock Text="姓名" Margin="10 10 10 0"/>

<TextBox Margin="10" />

<TextBlock Text="身份证" Margin="10 10 10 0"/>

<TextBox Margin="10" />

<TextBlock Text="地址" Margin="10 10 10 0"/>

<TextBox Margin="10" />

<Button Content="保存" Margin="10 10 10 30" VerticalAlignment="Bottom" HorizontalAlignment="Left" />

</StackPanel>

<StackPanel Grid.Row="1" Grid.Column="2">

<ListView />

<TextBlock Text="添加新预约" />

<DatePicker Margin="10" />

<Button Content="预约" />

</StackPanel>

</Grid>

11-8 【UI】组件化布局

如何把上节的 <!--header--> 提取出来,通过 UserControl 封装为可复用的UI组件。

在文件夹""右击 - Add - New Item ,左侧选择 WPF - User Control (WPF) ,命名为:HeaderControl

--\Controls\HeaderControl.xaml

<UserControl x:Class="WPF_CMS.Controls.HeaderControl"

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

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

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:local="clr-namespace:WPF_CMS.Controls"

mc:Ignorable="d"

d:DesignHeight="450" d:DesignWidth="800">

<Border Background="#7f3089">

<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">

<Image Height="90" Margin="5" Source="/Images/logo.jpg"/>

<TextBlock Text="WPF客户管理系统" FontSize="40" VerticalAlignment="Center" Foreground="#ffffff"/>

</StackPanel>

</Border>

</UserControl>

--\WPF_CMS\MainWindow.xaml

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

xmlns:controls="clr-namespace:WPF_CMS.Controls"

mc:Ignorable="d"

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

<Grid>

............

<!--header-->

<controls:HeaderControl Grid.ColumnSpan="3"/>

............

</Grid>

</Window>

修改后,未编译时代码显示依然报错,重新构建(编译)一下代码就可以了。

相关推荐
听麟1 小时前
HarmonyOS 6.0+ APP AR文旅导览系统开发实战:空间定位与文物交互落地
人工智能·深度学习·华为·ar·wpf·harmonyos
草莓熊Lotso3 小时前
Qt 主窗口核心组件实战:菜单栏、工具栏、状态栏、浮动窗口全攻略
运维·开发语言·人工智能·python·qt·ui
Coder_Boy_5 小时前
技术发展的核心规律是「加法打底,减法优化,重构平衡」
人工智能·spring boot·spring·重构
御承扬14 小时前
鸿蒙NDK UI之文本自定义样式
ui·华为·harmonyos·鸿蒙ndk ui
聆风吟º16 小时前
CANN hccl 深度解析:异构计算集群通信库的跨节点通信与资源管控实现逻辑
人工智能·wpf·transformer·cann
一起养小猫16 小时前
Flutter for OpenHarmony 实战_魔方应用UI设计与交互优化
flutter·ui·交互·harmonyos
无心水1 天前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
会一点设计1 天前
6个优质春节海报模板网站推荐!轻松设计马年祝福海报
ui·ux
一枕眠秋雨>o<1 天前
算子即战略:CANN ops-nn 如何重构大模型推理的内存与计算边界
人工智能·重构
LZL_SQ1 天前
HCCL测试框架中AllReduce边界条件测试设计深度剖析
wpf·cann