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 中定义 DataGridRowStyle

使用多个 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]:绑定到 DataRowViewRow 属性中的 Status 列。
  • 每个 DataTrigger 针对 Status 列的不同值设置不同的背景颜色。
  • 你可以根据需要添加更多的 DataTrigger,为更多的状态设置不同的颜色。

这样,你可以通过修改 DataTable 中的状态值,动态控制 DataGrid 中行的颜色变化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/54062.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Kubernetes】常见面试题汇总(十五)

目录 44.简述 Kubernetes RBAC 及其特点&#xff08;优势&#xff09;&#xff1f; 45.简述 Kubernetes Secret 作用&#xff1f; 46.简述 Kubernetes Secret 有哪些使用方式&#xff1f; 47.简述 Kubernetes PodSecurityPolicy 机制&#xff1f; 44.简述 Kubernetes RBAC 及…

基于Python实现一个庆祝中秋节的小程序

功能包括&#xff1a; 使用复杂的库来计算农历日期&#xff1a;可以使用 lunarcalendar 库来计算农历日期。提供更多的祝福语&#xff1a;可以通过随机选择祝福语来增加趣味性。加入图形用户界面 (GUI)&#xff1a;可以使用 tkinter 库来创建一个简单的图形用户界面。 我们可…

2024年Java后端学习路线

思维导图&#xff1a; 必备知识&#xff1a; Java基础 JavaWeb 数据库&#xff1a;MySql&#xff0c;Redis 开发中间件&#xff1a;Maven &#xff0c;Git &#xff0c;Docker&#xff0c;RabbitMQ 开发框架&#xff1a;SSM&#xff0c;spring boot&#xff0c;mybatis-plus、s…

十一、DMSP/OLS、NPP/VIIRS等夜间灯光数据之GDP空间化——新方法理论介绍

一、前言 之前的空间理论方法是将第一产业GDP和第二、三产业GDP分开,第一产业GDP和耕地面积进行反演,第二、三产业GDP和夜间灯光指数进行拟合,或者干脆不划分产业,就是第一、二、三产业gdp数据和夜间灯指数拟合。之前给大家介绍都是这种,那么现在很多文献提出一种新的做法…

JVM 运行时数据区域

目录 前言 程序计数器 java虚拟机栈 本地方法栈 java堆 方法区 运行时常量池 前言 首先, java程序在被加载在内存中运行的时候, 会把他自己管理的内存划分为若干个不同的数据区域, 就比如你是一个你是一个快递员, 一堆快递过来需要你分拣, 这个时候, 你就需要根据投放的目…

git常用命令(patch补丁和解决冲突)

diff/apply方案 使用diff命令生成patch文件&#xff0c;后使用apply命令应用patch到分支&#xff0c;从而实现修改复刻。 生成补丁 git diff > commit.patch 检查补丁 git apply --check commit.patch 应用补丁 git apply commit.patchgit diff --cached > commit.pa…

[产品管理-23]:NPDP新产品开发 - 21 - 产品创新中的市场调研 - 市场调研对创新产品开发的意义

目录 前言&#xff1a; 一、市场调研概述 1.1 客户与市场的区别 1、定义与范围 2、关注焦点 3、作用与影响 4、总结 1.2 销售与市场的区别 1、对象与范围 2、工作方式 3、工作内容 4、目标 5、考核标准 6、在企业运营中的角色 1.3 什么是市场调研 1、市场调研的…

COMDEL电源维修CLX2500康戴尔射频电源维修

美国COMDEL射频电源维修常见型号包括&#xff1a;CLX2750&#xff1b;CLX2500&#xff1b;CLX-600H&#xff1b;CX600AS&#xff1b;CX-5000S&#xff1b;CX-3500S&#xff1b;CX-2500S&#xff1b;CV500&#xff1b;CDX2000等。 Comdel成立于1966年&#xff0c;总部设在马萨诸…

Golang | Leetcode Golang题解之第406题根据身高重建队列

题目&#xff1a; 题解&#xff1a; func reconstructQueue(people [][]int) (ans [][]int) {sort.Slice(people, func(i, j int) bool {a, b : people[i], people[j]return a[0] > b[0] || a[0] b[0] && a[1] < b[1]})for _, person : range people {idx : pe…

性能测试:Locust使用介绍(三)

配置 Locust的配置&#xff0c;可以通过三种方式进行。第一种是命令行执行选项&#xff0c;第二种是将配置写到环境变量中&#xff0c;第三种是通过配置文件进行配置。 命令行选项 locust --helpUsage: locust [options] [UserClass ...]常用选项:-h, --help 显示…

Linux 基础入门操作-实验一 GCC使用

Linux 基础入门 前言 1 串口登录 本次登录可以采用串口登录&#xff0c;用usb线接入到系统&#xff0c;利用串口平台进行登录&#xff1b; 2 网口登录 2.1 路由器转接 电脑和开发板都接入路由器&#xff0c;路由器自动分开ip地址&#xff1b; 利用ipscan这个软件&#xf…

mysql DBA常用的sql

是否一般查询日志&#xff0c;默认关闭 show variables like ‘general_log’; 是否开启慢日志查询 默认关闭 show global variables like ‘slow_query_log’; 开启慢日志查询 SET GLOBAL slow_query_log ‘ON’; 默认是10 单位s SELECT long_query_time; 设置超过1s就算…

maya-vray渲染蒙版

要用一个叫vrayMulWrapper的材质球&#xff0c;把alpha Conterbution调到-1&#xff0c;勾选matte surface启用蒙版物体。

VLDB 超级大表上没有index 还需要gather statistics吗?

文档指明这个问题不属于DBA的问题,而是属于DW的问题,超大表的情况,statistics 要分析,而且不能留给夜晚的job去跑。 Do I need to gather statistics on an Oracle table that has no index? I have an Oracle 11g DB, and some of the tables are truncated and re-impo…

【GESP】C++一级练习BCQM3005,基本输出语句printf

一道基础练习题&#xff0c;练习基本输出语句printf。 BCQM3005 题目要求 描述 输出表达式1234∗5678的结果。 输入 无 输出 1234∗56787006652 输入样例 无 输出样例 1234 * 5678 7006652 全文详见个人独立博客&#xff1a;https://www.coderli.com/gesp-1-bcqm3005/ 【…

在树莓派上构建和部署 Node.js 项目

探索在Raspberry Pi上构建和部署Node.js项目的最佳实践。通过我们的专业提示和技巧&#xff0c;克服常见挑战&#xff0c;使您的项目顺利运行。 去年圣诞节&#xff0c;我收到了一份极其令人着迷的礼物&#xff0c;它占据了我许多周末的时间&#xff0c;甚至让我夜不能寐。它就…

零基础考过软考信息系统项目管理师经验分享

选择适合的课程&#xff1a;如果你是零基础&#xff0c;建议找一些专门针对新手的课程&#xff0c;讲解通俗易懂。 刷题至关重要&#xff1a;软考的题库很庞大&#xff0c;多做题是必须的。 做好笔记和复习&#xff1a;上课时要做好笔记&#xff0c;课后及时复习&#xff0c;…

网络安全学习(二)初识kali

kali有两种界面模式&#xff0c;为了更好的适应windows用户&#xff0c;需要操作一下。 先更新一下kali&#xff0c;执行命令 sudo apt-get update 然后换界面 sudo apt install kali-desktop-gnome 等待&#xff0c;出现如下界面时&#xff0c;选择gdm3&#xff08;键盘&a…

Mybatis中Like模糊查询三种处理方式

目录 Mybatis中Like模糊查询三种处理方式 1.通过单引号拼接${} 1&#xff09;mapper接口 2&#xff09;Mapper.xml 3&#xff09;测试代码 4) 测试结果 2.通过concat()函数拼接(个人推荐使用这种) 1&#xff09;mapper接口 2&#xff09;Mapper.xml 3&#xff09;测试代码 4) 测…

jvm学习记录01

目录标题 jvm和java的体系结构架构模型jvm生命周期启动执行结束 hotspot jvm和java的体系结构 架构模型 1基于栈的指令集架构2基于寄存器的指令集架构 两者区别&#xff1a; 基于栈式架构的特点 设计和实现更简单&#xff0c;适用于资源受限的系统; 避开了寄存器的分配难题:…