V.CodeGenerator WPF代码生成器--WPF自定义窗体
前言
前言:
受 WTM 的影响,想自己尝试写一个自动生成WPF项目的代码生成器
本文主要用于介绍基础库中WPF自定义窗体的使用。
作者的功底还不是很成熟,请大家多多包涵。
一、引用Vampirewal.Core基础库
二、使用
基础库中的自定义窗体已经在代码中进行了样式的引用,在窗体的Resources中,无需再进行引用,只需申明xmlns:corewindow=“Vampirewal.Windows”
即可
1.WindowBase
样式(示例):
代码如下(示例):
<corewindow:WindowBase --->此处将默认的Window改为corewindow:WindowBase(corewindow为我在下面自己命名的,可随意)
x:Class="test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-----↓↓↓此处需引用,前面命名随意
xmlns:corewindow="clr-namespace:Vampirewal.Core.WpfTheme.WindowStyle;assembly=Vampirewal.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:test"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
CloseWindowCommand="{Binding CloseWindowCommand}" //2022-3-10 新增
CloseWindowCommandParameter="{Binding ElementName=Main}" //2022-3-10 新增
Background="Orange"
mc:Ignorable="d">
<corewindow:WindowBase.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</corewindow:WindowBase.Resources>
<Grid>
</Grid>
</corewindow:WindowBase>
基础属性:
- IsOpenWindowSize:是否启用窗体大小调整,开启之后可任意拉伸窗体
- IsShowMinButton:是否显示最小化按钮,可酌情用于特殊情况下的窗体使用
- IsShowMaxButton:是否显示最大化和恢复窗体按钮
- TitleFontSize:可自定义设置窗体标题文字大小
自定义顶部信息栏
显示位置如图:将会在顶部靠右的位置,顺序横向排列
使用方式:(与Resources处于同一级)
<CoreWindow:WindowBase.TopCustomButtons>
<Button
Command="{Binding ReturnUpWindowCommand}"
Style="{StaticResource TransparentButton}"
ToolTip="返回首页">
<Path
Name="content"
Width="20"
Height="20"
Data="{StaticResource TransfromLeft}"
Fill="#74787c"
RenderTransformOrigin="0.5,0.5"
Stretch="Fill" />
</Button>
</CoreWindow:WindowBase.TopCustomButtons>
自定义底部信息栏
显示位置如图:在窗体底部最高50像素,从左往右顺序横向排列
使用方式:(与Resources处于同一级)
<CoreWindow:WindowBase.BottomCustomAreas>
<TextBlock
FontSize="20"
Foreground="White"
Text="我是底部信息栏" />
</CoreWindow:WindowBase.BottomCustomAreas>
2.MainWindowBase(适用于主页面的窗体)
继承自WindowBase,故WindowBase中的属性,该窗体依然可用。
新增属性:
- LeftMenuMaxWidth:左侧菜单栏最大宽度(默认值230)
- MainAreaHeight:顶部模块高度(默认值为0)
用法:
//左侧菜单栏
<corewindow:MainWindowBase.LeftContent>
<Border Background="Yellow" />
</corewindow:MainWindowBase.LeftContent>
//顶部模块区域
<corewindow:MainWindowBase.MainAreas>
<Button Content="test" />
</corewindow:MainWindowBase.MainAreas>
//左侧菜单栏和主内容区域的GridSplitter的ControlTemplate 自定义
<corewindow:MainWindowBase.GridSplitterStyle>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<Border
Margin="1,3"
Background="Red"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="2" />
</ControlTemplate>
</corewindow:MainWindowBase.GridSplitterStyle>
- 如果顶部模块区域没有任何控件,那么框架会自动设置高度为0
- 如果左侧菜单栏区域没有任何内容,那么框架会自动隐藏左侧和GridSplitter
- GridSplitter添加有双击功能,可自行尝试
- 剩余的和上文中的WindowBase用法一样
3.LayoutUcViewBase(布局界面基类)
需申明xmlns:coreuc=“Vampirewal.UcView”
直接上使用代码,上面是展示出来的截图
<coreuc:LayoutUcViewBase
x:Class="test.test_NewPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:coreuc="Vampirewal.UcView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:test"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding Source={StaticResource Locator}, Path=test_NewPageViewModel}"
mc:Ignorable="d">
<!-- 底部View -->
<coreuc:LayoutUcViewBase.BottomArea>
<Border Background="Red">
<ScrollViewer>
<StackPanel>
<TextBlock Text="hahahahahahahahahahahahahaha" />
<TextBlock Text="hahahahahahahahahahahahahaha" />
<TextBlock Text="hahahahahahahahahahahahahaha" />
</StackPanel>
</ScrollViewer>
</Border>
</coreuc:LayoutUcViewBase.BottomArea>
<!-- 自定义GridSplitter样式 -->
<coreuc:LayoutUcViewBase.GridSplitterStyle>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<Border
Margin="1,3"
Background="Yellow"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="2" />
</ControlTemplate>
</coreuc:LayoutUcViewBase.GridSplitterStyle>
<!-- 主要内容 -->
<Grid
Height="300"
Background="White"
Opacity="0.5" />
</coreuc:LayoutUcViewBase>
可以结合Dialog.ShowDialogWindow来食用!
4.AddOrEditUcViewBase(添加或修改页面基类)
需申明xmlns:CoreUc=“Vampirewal.UcView”
上面是截图
这个基类比较简单,只是给表单类型的页面提供一个基础的布局。对应的ViewModel使用BaseCURD,写了一个虚方法SaveCommand可以重写
<CoreUc:AddOrEditUcViewBase x:Class="test.TestAddorEditView"
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:CoreUc="Vampirewal.UcView"
xmlns:local="clr-namespace:test"
mc:Ignorable="d" DataContext="{Binding Source={StaticResource Locator},Path=TestAddorEditViewModel}"
d:DesignHeight="450" d:DesignWidth="800">
<CoreUc:AddOrEditUcViewBase.BottomBtnItems>
<Button Content="保存" Style="{StaticResource ButtonSuccess}" Command="{Binding SaveCommand}" Margin="5,0"/>
</CoreUc:AddOrEditUcViewBase.BottomBtnItems>
<Grid Background="White">
</Grid>
</CoreUc:AddOrEditUcViewBase>