news 2026/9/20 1:45:41

WPF之配方模块

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
WPF之配方模块

WPF之配方模块

一 UI设计

<!--配方界面--><TabItemHeader="配方"><Grid><Grid.ColumnDefinitions><ColumnDefinitionWidth="2*"/><ColumnDefinitionWidth="6*"/><ColumnDefinitionWidth="2*"/></Grid.ColumnDefinitions><StackPanel Grid.Column="0"><LabelContent="选择配方"></Label><ComboBoxx:Name="cbb_SelectRecipe"></ComboBox></StackPanel><extoolkit:PropertyGridName="propertyGrid_RecipeParams"Background="AliceBlue"Grid.Column="1"></extoolkit:PropertyGrid><StackPanel Grid.Column="2"><ButtonContent="加载配方"Margin="10"Command="{Binding btn_LoadRecipe_Command}"></Button><ButtonContent="新建配方"Margin="10"Command="{Binding btn_CreateRecipe_Command}"></Button><ButtonContent="删除配方"Margin="10"Command="{Binding btn_DeleteRecipe_Command}"></Button><ButtonContent="保存配方"Margin="10"Command="{Binding btn_SaveRecipe_Command}"></Button><ButtonContent="另存为配方"Margin="10"Command="{Binding btn_SaveAsRecipe_Command}"></Button></StackPanel></Grid></TabItem>

二 系统参数设置界面

<LabelContent="切换配方:"Margin="10"/><ComboBoxx:Name="cbb_SelectedRecipe"Text="{Binding AppParameter.RecipeName}"Margin="10"/><Buttonx:Name="btn_ChangeRecipe"Content="切换配方"Command="{Binding ChangeRecipeCommand}"/>

三 新建配方

UI

<mah:MetroWindowx:Class="AOI缺陷检测软件0103.RecipeView"xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"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:AOI缺陷检测软件0103"mc:Ignorable="d"WindowStartupLocation="CenterScreen"Title="配方名称"Height="140"Width="180"><StackPanel><StackPanelOrientation="Horizontal"Margin="10"><LabelContent=" 配方名称:"/><TextBoxx:Name="txt_RecipeName"Width="100"Text="配方1"/></StackPanel><ButtonMargin="10"Content="确认"Click="Button_Click"/></StackPanel></mah:MetroWindow>

后端

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingHelper;usingMahApps.Metro.Controls;namespaceAOI缺陷检测软件0103{/// <summary>/// RecipeView.xaml 的交互逻辑/// </summary>publicpartialclassRecipeView:MetroWindow{publicRecipeView(){InitializeComponent();}privatevoidButton_Click(objectsender,RoutedEventArgse){ZWModelzWModel=newZWModel();// 保存XMLXmlSerializerHelper.WriteXML(zWModel,Path.Combine("./Recipes",txt_RecipeName.Text.Trim()+".xml"),typeof(ZWModel));// 更新UIApplication.Current.Dispatcher.BeginInvoke((Action)(()=>{// 1.获取到_mainWindow窗口var_mainWindow=Application.Current.Windows.Cast<Window>().FirstOrDefault(window=>windowisMainWindow)asMainWindow;// 更新配方列表// 清除已有的_mainWindow.cbb_SelectRecipe.Items.Clear();// 清除已有的_mainWindow.cbb_SelectedRecipe.Items.Clear();// 获取所有配方xml文件string[]filePath=Directory.GetFiles("./Recipes");// 显示到主控件上foreach(stringiteminfilePath){// 获取文件名称stringRecipeName=Path.GetFileName(item);// 加到下拉框里_mainWindow.cbb_SelectRecipe.Items.Add(RecipeName);_mainWindow.cbb_SelectedRecipe.Items.Add(RecipeName);}}));// 关闭窗口this.Close();}}}

主界面 新建配方命令

#region新建配方publicRelayCommandbtn_CreateRecipe_Command{get{returnnewRelayCommand(CreateRecipe);}}publicvoidCreateRecipe(){RecipeViewrecipeView=newRecipeView();recipeView.ShowDialog();}#endregion

四 选择配方显示

<StackPanel Grid.Column="0"><LabelContent="选择配方"></Label><ComboBoxx:Name="cbb_SelectRecipe"DropDownClosed="cbb_SelectRecipe_DropDownClsed"></ComboBox></StackPanel>
privatevoidcbb_SelectRecipe_DropDownClsed(objectsender,EventArgse){stringrecipe=cbb_SelectRecipe.Text;ZWModelzWModel=(ZWModel)XmlSerializerHelper.ReadXML(Path.Combine("./Recipes",recipe),typeof(ZWModel));propertyGrid_RecipeParams.SelectedObject=zWModel;}

五 加载配方显示

// 更新配方列表string[]filePath=Directory.GetFiles("./Recipes");foreach(stringiteminfilePath){stringRecipeName=Path.GetFileName(item);cbb_SelectRecipe.Items.Add(RecipeName);cbb_SelectedRecipe.Items.Add(RecipeName);}

五 当前使用配方的显示和保存(已完成)

<ComboBoxx:Name="cbb_SelectedRecipe"Text="{Binding AppParameter.RecipeName}"Margin="10"/>
/// <summary>/// 保存软件参数命令/// </summary>publicRelayCommand<AppParameter>SaveAppParameterCommand{get{returnnewRelayCommand<AppParameter>(appParameter=>SaveAppParameter(appParameter));}}/// <summary>/// 保存软件参数/// </summary>publicvoidSaveAppParameter(AppParameterappParameter){// 保存参数XmlSerializerHelper.WriteXML(appParameter,@".\\AOI外观检测软件参数\\AppParameter.xml",typeof(AppParameter));MessageBox.Show("保存成功!");}

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/9/13 8:58:54

这么VIP文章,也没赚钱,有没有办法取消

写文章&#xff0c;主要是一种分享&#xff0c;共同进步。后来一些文章作为VIP&#xff0c;我也没意见。如果能赚钱补贴家用&#xff0c;也是好的。结果发现我这么多VIP文章&#xff0c;实际上没赚钱。几年下来也就几块钱。有一位老兄一次给了我16&#xff0c;也没法提现。两头…

作者头像 李华
网站建设 2026/9/14 16:40:32

【WRF-VPRM工具】WRF-GHG-Prepy 详解

目录 WRF-GHG-Prepy 仓库简介 环境配置 功能模块一览 A. 生物源排放(Biogenic Emissions) A1. Kaplan 模型(用于甲烷 CH₄) A2. VPRM 输入 B. 人为源排放(Anthropogenic Emissions) B1. EDGAR 排放数据(CO、CO₂、CH₄) B2. WetCHARTs 湿地甲烷排放数据 C. 火源排放(F…

作者头像 李华
网站建设 2026/9/8 8:58:33

影像之眼:人工智能如何重塑医学诊断的边界

个人首页&#xff1a; VON 鸿蒙系列专栏&#xff1a; 鸿蒙开发小型案例总结 综合案例 &#xff1a;鸿蒙综合案例开发 鸿蒙6.0&#xff1a;从0开始的开源鸿蒙6.0.0 鸿蒙5.0&#xff1a;鸿蒙5.0零基础入门到项目实战 本文章所属专栏&#xff1a;《AI从0到1&#xff1a;普通人…

作者头像 李华
网站建设 2026/9/15 13:46:40

智启未来:人工智能如何重塑高等教育新生态

个人首页&#xff1a; VON 鸿蒙系列专栏&#xff1a; 鸿蒙开发小型案例总结 综合案例 &#xff1a;鸿蒙综合案例开发 鸿蒙6.0&#xff1a;从0开始的开源鸿蒙6.0.0 鸿蒙5.0&#xff1a;鸿蒙5.0零基础入门到项目实战 本文章所属专栏&#xff1a;《AI从0到1&#xff1a;普通人…

作者头像 李华