wpf触发与模板的使用示例:批量生产工具
批量生产工具

<Window x:Class="WpfM20UpdateFW.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:WpfM20UpdateFW"mc:Ignorable="d"Title="FT_MultiUart Tool V2.7 " Height="600" Width="800" WindowState="Maximized" Closed="Window_Closed" KeyDown="Window_KeyDown" Loaded="Window_Loaded"><Window.Resources><RoutedUICommand x:Key="download" Text="download"/><RoutedUICommand x:Key="download_concel" Text="download_concel"/><RoutedUICommand x:Key="settings" Text="settings"/></Window.Resources><Window.InputBindings><KeyBinding Gesture="Alt+D" Key="D" Command="{StaticResource download}"></KeyBinding><KeyBinding Gesture="Alt+C" Key="C" Command="{StaticResource download_concel}"></KeyBinding><KeyBinding Gesture="Alt+S" Key="S" Command="{StaticResource settings}"></KeyBinding></Window.InputBindings><Window.CommandBindings><CommandBinding Command="{StaticResource download}" CanExecute="Button_Click_Download"></CommandBinding><CommandBinding Command="{StaticResource download_concel}" CanExecute="Button_Click_DownloadCancel"></CommandBinding><CommandBinding Command="{StaticResource settings}" CanExecute="Button_Click_Settings"></CommandBinding></Window.CommandBindings><Grid ShowGridLines="false" MinWidth="20" FocusManager.FocusedElement="{Binding ElementName=download}" Grid.ColumnSpan="4"><Grid.RowDefinitions><RowDefinition Height="30"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="0.1*"></ColumnDefinition><ColumnDefinition Width="0.1*"></ColumnDefinition><ColumnDefinition Width="0.1*"></ColumnDefinition><ColumnDefinition Width="0.1*"></ColumnDefinition></Grid.ColumnDefinitions><!--<TextBox Grid.Row="0" Grid.Column="0" x:Name="Path" IsReadOnly="True"/>--><Button Grid.Row="0" Grid.Column="0" FontSize="20" FontWeight="Bold" Margin="5,0,5,0" x:Name="settings" Click="Button_Click_Settings">Settings</Button><Button Grid.Row="0" Grid.Column="3" FontSize="20" FontWeight="Bold" Margin="50,0,5,0" x:Name="download" IsDefault="True" Click="Button_Click_Download" >Start</Button><TextBlock Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" x:Name="textBlockDevice" FontFamily="黑体" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding DeviceCount}"/><ListBox x:Name="myListBox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"><ListBox.ItemsPanel><ItemsPanelTemplate><WrapPanel Orientation="Horizontal" IsItemsHost="True"/></ItemsPanelTemplate></ListBox.ItemsPanel><ListBox.ItemTemplate><DataTemplate><StackPanel x:Name="myStackPanel" Orientation="Vertical" Background="AliceBlue" Margin="1 0 0 0" Width="300" Height="{Binding ActualHeight, ElementName=myListBox, Mode=OneWay}"><TextBlock x:Name="myCom" FontFamily="黑体" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock.Text ><MultiBinding StringFormat="{}COM{0,-4}"><Binding Path="Name"/></MultiBinding></TextBlock.Text></TextBlock><DataGrid x:Name="myDataGrid" AutoGenerateColumns="True" MaxHeight="400" ItemsSource="{Binding DataGridCollection}"><DataGrid.RowStyle><Style TargetType="DataGridRow"><Setter Property="Background" Value="White" /><Style.Triggers><!-- 这里是根据条件来设置颜色,假设你想要将满足某个条件的行背景设置为绿色 --><DataTrigger Binding="{Binding Row[状态]}" Value="PASS"><Setter Property="Background" Value="Green" /></DataTrigger><DataTrigger Binding="{Binding Row[状态]}" Value="FAIL"><Setter Property="Background" Value="Red" /></DataTrigger><DataTrigger Binding="{Binding Row[状态]}" Value="SKIP"><Setter Property="Background" Value="Gray" /></DataTrigger></Style.Triggers></Style></DataGrid.RowStyle></DataGrid><TextBlock x:Name="myStatus" Text="{Binding Path=Status}" FontFamily="黑体" FontSize="40" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock.Style><Style TargetType="TextBlock"><Setter Property="Foreground" Value="White" /><Style.Triggers><!-- 这里是根据条件来设置颜色,假设你想要将满足某个条件的行背景设置为绿色 --><DataTrigger Binding="{Binding Status}" Value="PASS"><Setter Property="Foreground" Value="Green" /></DataTrigger><DataTrigger Binding="{Binding Status}" Value="FAIL"><Setter Property="Foreground" Value="Red" /></DataTrigger><DataTrigger Binding="{Binding Status}" Value="SKIP"><Setter Property="Foreground" Value="Gray" /></DataTrigger><DataTrigger Binding="{Binding Status}" Value="READY"><Setter Property="Foreground" Value="Black" /></DataTrigger></Style.Triggers></Style></TextBlock.Style></TextBlock><TextBox x:Name="myLog" Text="{Binding Path=Log}" MinHeight="100" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AcceptsReturn="True" IsReadOnly="True" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" /></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox><!--<ListView x:Name="myListBox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="4"><ListView.ItemsPanel><ItemsPanelTemplate><WrapPanel Orientation="Horizontal" IsItemsHost="True"/></ItemsPanelTemplate></ListView.ItemsPanel><ListView.ItemTemplate><DataTemplate><StackPanel Orientation="Vertical" Background="AliceBlue" Margin="1 0 0 0" Width="300" Height="{Binding ActualHeight, ElementName=myListBox, Mode=OneWay}"><TextBlock x:Name="textBlock" FontFamily="黑体" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock.Text ><MultiBinding StringFormat="{}COM{0,-4}"><Binding Path="Name"/></MultiBinding></TextBlock.Text></TextBlock><DataGrid Name="myDataGrid" AutoGenerateColumns="True" MaxHeight="500" /><TextBlock Text="{Binding Path=Status}" Foreground="{Binding Path=StatusForeground}" FontFamily="黑体" FontSize="40" HorizontalAlignment="Center" VerticalAlignment="Center"/><TextBox x:Name="logText" MinHeight="100" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" AcceptsReturn="True" IsReadOnly="True" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" /></StackPanel></DataTemplate></ListView.ItemTemplate>--><!-- 添加数据绑定项 --><!--</ListView>--><!--<ListBox DockPanel.Dock="Top" x:Name="myListBoxSP" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center"><ListBox.ItemTemplate><DataTemplate><Grid><ProgressBar x:Name="Progress"><ProgressBar.Value><Binding Path="ProgressValue"></Binding></ProgressBar.Value><ProgressBar.Foreground><Binding Path="ProgressForeground"></Binding></ProgressBar.Foreground></ProgressBar><TextBlock x:Name="textBlock" FontFamily="黑体" FontSize="25" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock.Text ><MultiBinding StringFormat="{}SP COM{0,-4}{1,4}%"><Binding Path="Name"/><Binding Path="Value" ElementName="Progress"/></MultiBinding></TextBlock.Text></TextBlock></Grid></DataTemplate></ListBox.ItemTemplate></ListBox>--></Grid>
</Window>
还是不习惯数据绑定的方式,总是想用getcontrol先获取控件,再对控件进行处理。我注释后,绑定也很方便!!!
private void Window_Loaded(object sender, RoutedEventArgs e)
{DataTable dataTable = new DataTable();dataTable.Columns.Add("序号", typeof(int));dataTable.Columns.Add("测试项", typeof(string));dataTable.Columns.Add("状态", typeof(string));dataTable.Columns.Add("值", typeof(string));for (int j = 1; j <= 40; j++){dataTable.Rows.Add(j, "读SN", j < 10?"PASS":"FAIL","FT123456789");}for (int i = 0; i < bindingList.Count; i++){//TextBlock myCom = (TextBlock)MyAPI.getControl(myListBox, i, "myCom");//DataGrid myDataGrid = (DataGrid)MyAPI.getControl(myListBox, i, "myDataGrid");//if (myDataGrid != null)//{// myDataGrid.ItemsSource = dataTable.DefaultView;//}bindingList[i].DataGridCollection = dataTable.DefaultView;//TextBlock myStatus = (TextBlock)MyAPI.getControl(myListBox, i, "myStatus");//myStatus.Text = "PASS";bool b = true;for (int k = 0; k < dataTable.Rows.Count; k++) {if (dataTable.Rows[k]["状态"].Equals("FAIL")){b = false;}}bindingList[i].Status = b?"PASS":"FAIL";//TextBlock myLog = (TextBlock)MyAPI.getControl(myListBox, i, "myLog");//myLog.Text = "测试完成";bindingList[i].Log = "测试完成";}
}
触发示例:多种状态对应不同颜色
DataTrigger 可以设置多种状态,每种状态对应不同的颜色。你可以为 DataGridRow 设置多个 DataTrigger,根据不同的条件更改行的颜色。假设你有一个 Status 列,不同的状态对应不同的颜色,比如 “Green”、“Yellow”、“Red” 等。
示例:多种状态对应不同颜色
假设 DataTable 中有一个 Status 列,根据 Status 的值来设置不同的背景颜色。
1. 创建 DataTable 并绑定到 DataGrid
// 创建 DataTable
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Status", typeof(string)); // 状态列,用于改变颜色// 添加示例数据
table.Rows.Add("Item 1", "Green"); // 对应绿色
table.Rows.Add("Item 2", "Yellow"); // 对应黄色
table.Rows.Add("Item 3", "Red"); // 对应红色// 绑定到 DataGrid
myDataGrid.ItemsSource = table.DefaultView;
2. 在 XAML 中定义 DataGrid 的 RowStyle
使用多个 DataTrigger 为不同的状态设置不同的行背景颜色:
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="True"><DataGrid.RowStyle><Style TargetType="DataGridRow"><Setter Property="Background" Value="White" /> <!-- 默认背景色 --><Style.Triggers><!-- 当 Status 为 "Green" 时,将行背景设置为绿色 --><DataTrigger Binding="{Binding Path=Row[Status]}" Value="Green"><Setter Property="Background" Value="LightGreen" /></DataTrigger><!-- 当 Status 为 "Yellow" 时,将行背景设置为黄色 --><DataTrigger Binding="{Binding Path=Row[Status]}" Value="Yellow"><Setter Property="Background" Value="Yellow" /></DataTrigger><!-- 当 Status 为 "Red" 时,将行背景设置为红色 --><DataTrigger Binding="{Binding Path=Row[Status]}" Value="Red"><Setter Property="Background" Value="Salmon" /></DataTrigger><!-- 你可以添加更多状态和对应的颜色 --></Style.Triggers></Style></DataGrid.RowStyle>
</DataGrid>
3. 动态修改 DataTable 行状态
你可以在代码中动态修改 Status 列的值来触发颜色变化:
// 将第一行的状态设置为 "Red",触发行背景变为红色
table.Rows[0]["Status"] = "Red";
解释
Path=Row[Status]:绑定到DataRowView的Row属性中的Status列。- 每个
DataTrigger针对Status列的不同值设置不同的背景颜色。 - 你可以根据需要添加更多的
DataTrigger,为更多的状态设置不同的颜色。
这样,你可以通过修改 DataTable 中的状态值,动态控制 DataGrid 中行的颜色变化。
相关文章:
wpf触发与模板的使用示例:批量生产工具
批量生产工具 <Window x:Class"WpfM20UpdateFW.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/expressio…...
brew install node提示:Error: No such keg: /usr/local/Cellar/node
打开本地文件发现Cellar目录下无法生成 node文件,应该是下载时出现问题,重复下载无法解决问题,只能重新安装brew。 步骤1(安装 brew): /bin/zsh -c “$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/ra…...
记录一下gitlab社区版的安装教程
目录 1.更新系统软件包 2.安装必要的依赖 3.添加GitLab源 3.1对于GitLab Enterprise Edition(EE): 3.2对于GitLab Community Edition(CE): 4.安装GitLab 4.1安装GitLab Enterprise Edition(E…...
20. 如何在MyBatis中处理多表关联查询?常见的实现方式有哪些?
在MyBatis中处理多表关联查询是一项常见的需求,特别是在关系型数据库中存储复杂的实体关系时。MyBatis提供了多种方式来实现多表关联查询,常见的实现方式包括使用<association>和<collection>标签在<resultMap>中进行对象关系映射&…...
【百日算法计划】:每日一题,见证成长(013)
题目 回文链表 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。 输入:head [1,2,2,1] 输出:true 思路 找到中间节点反转后半部分链表前后链表顺序比…...
PCL 读取和保存点云
目录 一、概述 1.1原理 1.2实现步骤 二、代码实现 2.1关键函数 2.2完整代码 三、实现效果 PCL点云算法汇总及实战案例汇总的目录地址链接: PCL点云算法与项目实战案例汇总(长期更新) 一、概述 1.1原理 PCL (Point Cloud Library) 是…...
js | TypeError: Cannot read properties of null (reading ‘indexOf’) 【解决】
js | TypeError: Cannot read properties of null (reading ‘indexOf’) 【解决】 描述 概述 在前端开发中,遇到TypeError: Cannot read properties of null (reading indexOf)这类错误并不罕见。这个错误通常表明你试图在一个null值上调用indexOf方法,…...
微信小程序-formData使用
作者:fyupeng 技术专栏:☞ https://github.com/fyupeng 项目地址:☞ https://github.com/fyupeng/distributed-blog-system-api 留给读者 一、介绍 在小程序中使用formdata上传数据,可实现多文件上传 跟浏览器中的FormData对象类…...
潜在语义分析(Latent Semantic Analysis,LSA)—无监督学习方法、非概率模型、判别模型、线性模型、非参数化模型、批量学习
定义 输入: X [ x 11 x 12 ⋯ x 1 n x 21 x 22 ⋯ x 2 n ⋮ ⋮ ⋮ ⋮ x m 1 x m 2 ⋯ x m n ] , 文本集合 D { d 1 , d 2 , ⋯ , d n } , 单词集合 W { ω 1 , ω 2 , ⋯ , ω m } , x i j : 单词 ω i 在文本 d j 中出现的频数或权值 X\left[ \begin{array}{cccc} x_{11} …...
【安全漏洞】MySQL 8.0.33 、CVE-2023-22102
mysql-connector-java:jar:8.0.33已经重新定位到mysql-connector-j:jar:8.0.33 安全漏洞描述 在SBOM扫描过程中,检测到mysql-connector-j:8.0.33存在如下高危安全漏洞: CVE-2023-22102:Oracle MySQL Connectors 8.1.0 版本之前存在安全漏洞&…...
Flutter 响应式框架
一、简介 响应式框架会自动使用户界面适应不同的屏幕大小。创建你的用户界面一次,让它显示完美的像素在移动,平板电脑和桌面! 1.1 问题 支持多种显示尺寸通常意味着要多次重新创建同一布局。在传统的Bootstrap方法下,构建响应式…...
电脑AE特效软件 After Effects软件2017中文版下载安装指南 (Win/Mac)
电脑ae特效软件 After Effects软件2017中文版下载安装win/... 电脑AE特效软件 After Effects软件2017中文版下载安装指南 (Win/Mac) Adobe After Effects 2017 是一款功能强大的视频后期处理软件,广泛应用于影视特效制作、动态图形设计、视觉效果合成等领域。其丰…...
C#中的装箱和拆箱是什么
在 C# 中,装箱(Boxing)和拆箱(Unboxing)是与值类型和引用类型相关的概念,涉及到值类型的数据在托管堆(Heap)上的存储方式。 装箱(Boxing) 装箱是指将值类型…...
在 Debian 12 上安装中文五笔输入法
在 Debian 12 上安装中文五笔输入法,你可以通过以下步骤进行: 更新系统包列表: 打开终端,首先更新你的系统包列表: sudo apt update安装输入法框架: 安装 fcitx5 输入法框架: sudo apt install …...
整流器制造5G智能工厂物联数字孪生平台,推进制造业数字化转型
整流器制造行业作为制造业的重要组成部分,也在积极探索数字化转型的新路径。整流器,作为电力电子领域的关键元件,广泛应用于通信、工业控制、新能源等多个领域,其制造过程的智能化升级不仅关乎产品性能的提升,更是推动…...
算法知识点——常用输入输出数据的方式
如果输入的每组数据的结果不相互干扰的话,就可以在本次操作的时候将该组数据的相关结果进行输出。 1、n组输入输出(n确定) scanf("%d",&n); while(n--) {scanf("%d %d",&a,&b); printf("%d %d\n",a,b);}cin…...
如何构建大数据治理平台,助力企业数据决策
建设背景 (1)什么是数据资产 资产由企业及组织拥有和控制,能够提供增值服务、带来经济利益的重要资源。 资产不但需要管理, 更需要运营。 (2)数据资产运营中的问题 数据资产运营中存在的问题主要包括以下…...
Playwright与Selenium的对比:谁是更适合你的自动化测试工具?
在自动化测试领域,Selenium 一直是行业的标杆工具。它功能强大、支持多浏览器、广泛应用于各类项目中。然而,随着技术的发展,新的工具不断涌现,Playwright 作为其中的佼佼者,以其现代化的设计和强大的特性吸引了越来越…...
Netty 相关问题
传统网络编程存在的问题 传统网络编程存在以下问题: 线程创建开销:在Java中,创建线程需要调用操作系统API,这会消耗资源和时间。内存占用高:线程本身占用内存,创建过多线程会导致内存资源紧张。CPU使用率…...
JAVA中线程池的详解
1.概念 顾名思义,线程池就是管理一系列线程的资源池,其提供了一种限制和管理线程资源的方式。每个线程池还维护一些基本统计信息,例如已完成任务的数量。 这里借用《Java 并发编程的艺术》书中的部分内容来总结一下使用线程池的好处&#x…...
IDEA运行Tomcat出现乱码问题解决汇总
最近正值期末周,有很多同学在写期末Java web作业时,运行tomcat出现乱码问题,经过多次解决与研究,我做了如下整理: 原因: IDEA本身编码与tomcat的编码与Windows编码不同导致,Windows 系统控制台…...
CVPR 2025 MIMO: 支持视觉指代和像素grounding 的医学视觉语言模型
CVPR 2025 | MIMO:支持视觉指代和像素对齐的医学视觉语言模型 论文信息 标题:MIMO: A medical vision language model with visual referring multimodal input and pixel grounding multimodal output作者:Yanyuan Chen, Dexuan Xu, Yu Hu…...
React hook之useRef
React useRef 详解 useRef 是 React 提供的一个 Hook,用于在函数组件中创建可变的引用对象。它在 React 开发中有多种重要用途,下面我将全面详细地介绍它的特性和用法。 基本概念 1. 创建 ref const refContainer useRef(initialValue);initialValu…...
Zustand 状态管理库:极简而强大的解决方案
Zustand 是一个轻量级、快速和可扩展的状态管理库,特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...
【Redis技术进阶之路】「原理分析系列开篇」分析客户端和服务端网络诵信交互实现(服务端执行命令请求的过程 - 初始化服务器)
服务端执行命令请求的过程 【专栏简介】【技术大纲】【专栏目标】【目标人群】1. Redis爱好者与社区成员2. 后端开发和系统架构师3. 计算机专业的本科生及研究生 初始化服务器1. 初始化服务器状态结构初始化RedisServer变量 2. 加载相关系统配置和用户配置参数定制化配置参数案…...
1688商品列表API与其他数据源的对接思路
将1688商品列表API与其他数据源对接时,需结合业务场景设计数据流转链路,重点关注数据格式兼容性、接口调用频率控制及数据一致性维护。以下是具体对接思路及关键技术点: 一、核心对接场景与目标 商品数据同步 场景:将1688商品信息…...
Linux云原生安全:零信任架构与机密计算
Linux云原生安全:零信任架构与机密计算 构建坚不可摧的云原生防御体系 引言:云原生安全的范式革命 随着云原生技术的普及,安全边界正在从传统的网络边界向工作负载内部转移。Gartner预测,到2025年,零信任架构将成为超…...
CMake控制VS2022项目文件分组
我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...
iOS性能调优实战:借助克魔(KeyMob)与常用工具深度洞察App瓶颈
在日常iOS开发过程中,性能问题往往是最令人头疼的一类Bug。尤其是在App上线前的压测阶段或是处理用户反馈的高发期,开发者往往需要面对卡顿、崩溃、能耗异常、日志混乱等一系列问题。这些问题表面上看似偶发,但背后往往隐藏着系统资源调度不当…...
【分享】推荐一些办公小工具
1、PDF 在线转换 https://smallpdf.com/cn/pdf-tools 推荐理由:大部分的转换软件需要收费,要么功能不齐全,而开会员又用不了几次浪费钱,借用别人的又不安全。 这个网站它不需要登录或下载安装。而且提供的免费功能就能满足日常…...
