Wpf美化按钮,输入框,下拉框,dataGrid

ops/2025/2/14 2:19:49/

Wpf美化按钮,输入框,下拉框,dataGrid

引用代码后

在这里插入图片描述

引用资源

<ControlTemplate x:Key="CustomProgressBarTemplate" TargetType="ProgressBar"><Grid><Border x:Name="PART_Track" CornerRadius="2" BorderBrush="#AAAAAA" BorderThickness="1"><Border.Background><LinearGradientBrush StartPoint="0,0" EndPoint="1,1"><GradientStop Color="#EEEEEE" Offset="0"/><GradientStop Color="#CCCCCC" Offset="1"/></LinearGradientBrush></Border.Background><Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" Fill="#248735"/></Border></Grid><ControlTemplate.Triggers><Trigger Property="IsIndeterminate" Value="True"><!-- 处理不确定模式的逻辑 --></Trigger></ControlTemplate.Triggers>
</ControlTemplate><Style x:Key="ToggleButtonStyle" TargetType="{x:Type ToggleButton}"><Setter Property="Width" Value="60"></Setter><Setter Property="Height" Value="26"></Setter><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="ToggleButton"><!--定义视觉树--><Border x:Name="border" BorderThickness="1" CornerRadius="12" BorderBrush="#aaa" Background="#2790ff"><Grid x:Name="togglebutton" HorizontalAlignment="Right"><Border Width="24" Height="24" CornerRadius="12" Background="White"/></Grid><!--阴影设置--><Border.Effect><DropShadowEffect Color="Gray" BlurRadius="5" ShadowDepth="0" Opacity="0.5" /></Border.Effect></Border><!--定义触发器--><ControlTemplate.Triggers><Trigger Property="IsChecked" Value="false"><Setter TargetName="border" Property="Background" Value="#ccc"/><Setter TargetName="togglebutton" Property="HorizontalAlignment" Value="Left"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter>
</Style>
<Style x:Key="pro1" TargetType="{x:Type ProgressBar}"><Setter Property="Height" Value="15"/><Setter Property="Background" Value="#F9F9F9"/><Setter Property="Padding" Value="5,2"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ProgressBar}"><Border Padding="0" CornerRadius="10" Background="{TemplateBinding Background}"><Grid Height="{TemplateBinding Height}"><Border x:Name="PART_Track"/><Grid  x:Name="PART_Indicator" Background="Transparent" HorizontalAlignment="Left" ><Border Background="Green" CornerRadius="10"><Viewbox HorizontalAlignment="Right"Margin="{TemplateBinding Padding}" SnapsToDevicePixels="True"><TextBlock Foreground="White"HorizontalAlignment="Right"Text="{Binding RelativeSource={RelativeSource TemplatedParent},Path=Value,StringFormat={}{0}%}"/></Viewbox></Border></Grid></Grid></Border><ControlTemplate.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="gray"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter>
</Style>
<Style x:Key="TextBoxStyle" TargetType="TextBox"><Setter Property="Foreground" Value="Black"/><Setter Property="BorderBrush" Value="#FF3C3C3C"/><Setter Property="BorderThickness" Value="1"/><Setter Property="Padding" Value="5"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="TextBox"><Border Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"CornerRadius="5"><ScrollViewer x:Name="PART_ContentHost"/></Border></ControlTemplate></Setter.Value></Setter><!-- 当 IsReadOnly 为 true 时,改变背景色 --><Style.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="LightGray"/><Setter Property="Foreground" Value="DarkGray"/><Setter Property="BorderBrush" Value="Gray"/></Trigger></Style.Triggers>
</Style><Style x:Key="ButtonStyle" TargetType="Button"><Setter Property="FontSize" Value="16"/><Setter Property="BorderBrush" Value="Transparent"/><Setter Property="Margin" Value="10"/><Setter Property="Foreground" Value="White"/><Setter Property="Background" Value="Green"/><Setter Property="Padding" Value="10,5"/><Setter Property="FontWeight" Value="Bold"/><Setter Property="FocusVisualStyle" Value="{x:Null}"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" CornerRadius="10"><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /></Border></ControlTemplate></Setter.Value></Setter><Style.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="LightGray"/><Setter Property="Foreground" Value="DarkGray"/><Setter Property="BorderBrush" Value="Gray"/></Trigger></Style.Triggers>
</Style><!-- 定义 ComboBox 的样式 -->
<Style x:Key="ComboBoxStyle" TargetType="ComboBox"><!-- 默认样式 --><Setter Property="Background" Value="White"/><Setter Property="Foreground" Value="#FF1E1E1E"/><Setter Property="BorderBrush" Value="#FF3C3C3C"/><Setter Property="BorderThickness" Value="1"/><Setter Property="Padding" Value="5"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="ComboBox"><Grid><Border x:Name="Border" Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"CornerRadius="5"/><ToggleButton x:Name="ToggleButton"Grid.Column="1"Focusable="False"IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"ClickMode="Press"><ToggleButton.Template><ControlTemplate TargetType="ToggleButton"><Border Background="Transparent"><Path x:Name="Arrow"Fill="Green"HorizontalAlignment="Right"VerticalAlignment="Center"Data="M 0 0 L 4 4 L 8 0 Z"/></Border></ControlTemplate></ToggleButton.Template></ToggleButton><ContentPresenter x:Name="ContentSite"IsHitTestVisible="False"Content="{TemplateBinding SelectionBoxItem}"ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"Margin="5,0,0,0"VerticalAlignment="Center"HorizontalAlignment="Left"/><TextBox x:Name="PART_EditableTextBox"Style="{x:Null}"HorizontalAlignment="Left"VerticalAlignment="Center"Margin="5,0,0,0"Focusable="True"Background="Transparent"Foreground="{TemplateBinding Foreground}"Visibility="Hidden"IsReadOnly="{TemplateBinding IsReadOnly}"/><Popup x:Name="Popup"Placement="Bottom"IsOpen="{TemplateBinding IsDropDownOpen}"AllowsTransparency="True"Focusable="False"PopupAnimation="Slide"><Grid x:Name="DropDown"SnapsToDevicePixels="True"MinWidth="{TemplateBinding ActualWidth}"MaxHeight="{TemplateBinding MaxDropDownHeight}"><Border x:Name="DropDownBorder"Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="1"CornerRadius="5"/><ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"><StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/></ScrollViewer></Grid></Popup></Grid></ControlTemplate></Setter.Value></Setter><!-- 当 IsEnabled 为 false 时,改变背景色 --><Style.Triggers><Trigger Property="IsEnabled" Value="False"><Setter Property="Background" Value="LightGray"/><Setter Property="Foreground" Value="DarkGray"/><Setter Property="BorderBrush" Value="Gray"/></Trigger></Style.Triggers>
</Style><Style x:Key="DataGridStyles" TargetType="DataGrid"><!--<Setter Property="ColumnHeaderStyle" Value="{DynamicResource ColumnHeaderStyle}"></Setter><Setter Property="CellStyle" Value="{DynamicResource CellStyle}"></Setter><Setter Property="RowStyle" Value="{DynamicResource RowStyle}"></Setter>--><!-- <Setter Property="EnableRowVirtualization" Value="True"></Setter> --><!-- <Setter Property="GridLinesVisibility" Value="None"></Setter> --><!-- <Setter Property="CanUserAddRows" Value="False"></Setter> --><!-- <Setter Property="AutoGenerateColumns" Value="False"></Setter> --><Setter Property="IsEnabled" Value="True"></Setter><Setter Property="HeadersVisibility" Value="Column"></Setter><!--网格线颜色--><Setter Property="Background" Value="White" />
</Style>
<Style x:Key="ColumnHeaderStyles" TargetType="DataGridColumnHeader"><Setter Property="Height" Value="35"></Setter><Setter Property="FontSize" Value="13"></Setter><Setter Property="Background" Value="#F2F2F2"></Setter><Setter Property="BorderThickness" Value="0,0,1,1"></Setter><Setter Property="BorderBrush" Value="Black"></Setter><Setter Property="VerticalContentAlignment" Value="Center"></Setter><Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
<Style x:Key="RowStyles" TargetType="DataGridRow"><Setter Property="Cursor" Value="Hand"></Setter><Setter Property="BorderBrush" Value="Black"/><Setter Property="BorderThickness" Value="0,0,0,1"/><Style.Triggers><Trigger Property="IsMouseOver" Value="true"><Setter Property="Background" Value="#F2F2F2"/></Trigger><Trigger Property="IsSelected" Value="True"><Setter Property="Background"  Value="Blue" /></Trigger></Style.Triggers>
</Style>
<Style x:Key="CellStyles" TargetType="DataGridCell"><Setter Property="Height" Value="35"></Setter><Setter Property="FontSize" Value="15"></Setter><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="DataGridCell"><Border x:Name="Bg" Background="Transparent" BorderThickness="0" UseLayoutRounding="True" BorderBrush="#FFCBCBCB"><ContentPresenter HorizontalAlignment="Center"  VerticalAlignment="Center" /></Border></ControlTemplate></Setter.Value></Setter>
</Style>
<!-- 定义数据表格的基础风格 --><Style x:Key="styleDataGridView" TargetType="DataGrid"><Setter Property="Background" Value="#F0F0F0"/><Setter Property="BorderBrush" Value="#D7D7D7"/><Setter Property="BorderThickness" Value="1"/><Setter Property="RowHeaderWidth" Value="25"/><Setter Property="AutoGenerateColumns" Value="False"/><Setter Property="CanUserAddRows" Value="False"/><!-- <Setter Property="AlternatingRowBackground" Value="#EDEDED"/> --><!-- 隔行变色效果 --> <Setter Property="CellStyle"><Setter.Value><Style TargetType="DataGridCell"><Setter Property="Padding" Value="8,4"/><Setter Property="Foreground" Value="Black"/><Setter Property="FontSize" Value="14"/><Setter Property="FontFamily" Value="Arial"/><Setter Property="HorizontalContentAlignment" Value="Center"/><Setter Property="VerticalContentAlignment" Value="Center"/></Style></Setter.Value></Setter></Style><Style x:Key="DataGridStyles" TargetType="DataGrid"><Setter Property="ColumnHeaderStyle" Value="{DynamicResource ColumnHeaderStyle}"></Setter><Setter Property="CellStyle" Value="{DynamicResource CellStyle}"></Setter><Setter Property="RowStyle" Value="{DynamicResource RowStyle}"></Setter><!-- <Setter Property="EnableRowVirtualization" Value="True"></Setter> --><!-- <Setter Property="GridLinesVisibility" Value="None"></Setter> --><!-- <Setter Property="CanUserAddRows" Value="False"></Setter> --><!-- <Setter Property="AutoGenerateColumns" Value="False"></Setter> --><Setter Property="IsEnabled" Value="True"></Setter><Setter Property="HeadersVisibility" Value="Column"></Setter><!--网格线颜色--><Setter Property="Background" Value="White" /></Style><Style x:Key="ColumnHeaderStyles" TargetType="DataGridColumnHeader"><Setter Property="Height" Value="35"></Setter><Setter Property="FontSize" Value="15"></Setter><Setter Property="FontWeight" Value="Bold"></Setter><Setter Property="Background" Value="#F2F2F2"></Setter><Setter Property="BorderThickness" Value="0,0,1,1"></Setter><Setter Property="BorderBrush" Value="Black"></Setter><Setter Property="VerticalContentAlignment" Value="Center"></Setter><Setter Property="HorizontalContentAlignment" Value="Center"></Setter></Style><Style x:Key="RowStyles" TargetType="DataGridRow"><Setter Property="Cursor" Value="Hand"></Setter><Setter Property="BorderBrush" Value="Black"/><Setter Property="BorderThickness" Value="0,0,0,1"/><Style.Triggers><Trigger Property="IsMouseOver" Value="true"><Setter Property="Background" Value="#F2F2F2"/></Trigger><Trigger Property="IsSelected" Value="True"><Setter Property="Background"  Value="Blue" /></Trigger></Style.Triggers></Style><Style x:Key="CellStyles" TargetType="DataGridCell"><Setter Property="Height" Value="35"></Setter><Setter Property="FontSize" Value="15"></Setter><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="DataGridCell"><Border x:Name="Bg" Background="Transparent" BorderThickness="0" UseLayoutRounding="True" BorderBrush="#FFCBCBCB"><ContentPresenter HorizontalAlignment="Center"  VerticalAlignment="Center" /></Border></ControlTemplate></Setter.Value></Setter></Style>

项目主要代码

MainWindow.xaml

<Window x:Class="TestWpfUI.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"mc:Ignorable="d"Title="MainWindow" Height="600" Width="1000"WindowStartupLocation="CenterScreen"><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition/></Grid.RowDefinitions><TextBox Grid.Row="0" Style="{StaticResource TextBoxStyle}" Width="200" Height="40"x:Name="textBox1"FontSize="20"></TextBox><Button Grid.Row="1" Style="{StaticResource ButtonStyle}" Width="100" Height="50" Content="美化按钮" Click="Button1_OnClick"/><StackPanel Grid.Row="2" Orientation="Horizontal"><TextBox  Style="{StaticResource TextBoxStyle}" Width="200" Height="40"x:Name="textBox2"IsReadOnly="True"FontSize="20"></TextBox><Button Style="{StaticResource ButtonStyle}" Width="100" Height="50" Content="美化按钮"IsEnabled="False"Click="Button2_OnClick"x:Name="Button2"/></StackPanel><StackPanel Grid.Row="3" Orientation="Horizontal"  VerticalAlignment="Center"><!-- 普通 ComboBox --><ComboBox x:Name="ComboBox1" FontSize="15" Style="{StaticResource ComboBoxStyle}" Width="200" Height="40" SelectedIndex="0"><ComboBoxItem Content="选项 1"/><ComboBoxItem Content="选项 2"/><ComboBoxItem Content="选项 3"/></ComboBox><!-- 不可用 ComboBox --><ComboBox x:Name="ComboBox2" FontSize="15" Style="{StaticResource ComboBoxStyle}" Width="200" Height="40"Margin="10,0,0,0" SelectedIndex="0" IsEnabled="False"><ComboBoxItem Content="选项 1"/><ComboBoxItem Content="选项 2"/><ComboBoxItem Content="选项 3"/></ComboBox></StackPanel><Grid Grid.Row="4"><DataGrid x:Name="DataGrids" Style="{StaticResource styleDataGridView}"><DataGrid.Columns><DataGridTextColumn Header="测试名称" Binding="{Binding Path=TestName ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.15*" /><DataGridTextColumn Header="测试命令" Binding="{Binding Path=Command,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.15*" /><DataGridTextColumn Header="测试时间" Binding="{Binding Path=TestDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.15*" /><DataGridTextColumn Header="测试结果" Binding="{Binding Path=TestValue,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="0.11*"><DataGridTextColumn.CellStyle ><Style TargetType="DataGridCell"><Setter Property="FontSize" Value="20"></Setter><Style.Triggers><DataTrigger Binding="{Binding TestValue}" Value="PASS"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="DataGridCell"><Border Background="Green"><ContentPresenter HorizontalAlignment="Center"  VerticalAlignment="Center" /></Border></ControlTemplate></Setter.Value></Setter><Setter Property="Foreground" Value="White"></Setter></DataTrigger><DataTrigger Binding="{Binding TestValue}" Value="FAIL"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="DataGridCell"><Border Background="Red" ><ContentPresenter HorizontalAlignment="Center"  VerticalAlignment="Center" /></Border></ControlTemplate></Setter.Value></Setter><Setter Property="Foreground" Value="White"></Setter></DataTrigger></Style.Triggers></Style></DataGridTextColumn.CellStyle></DataGridTextColumn></DataGrid.Columns></DataGrid></Grid></Grid>
</Window>

MainWindow.cs

using System.Collections.ObjectModel;
using System.Windows;
using TestWpfUI.Common.Models;namespace TestWpfUI;/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{public MainWindow(){InitializeComponent();var testDatas = new ObservableCollection<TestData>();for (int i = 1; i <= 10; i++){testDatas.Add(new TestData() { TestName = "测试"+i, Command = "命令"+i,TestDate = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss"),TestValue = "PASS"});}DataGrids.ItemsSource = testDatas;}private void Button1_OnClick(object sender, RoutedEventArgs e){MessageBox.Show(textBox1.Text);if (textBox2.IsReadOnly){textBox2.IsReadOnly = false;}if (Button2.IsEnabled == false){Button2.IsEnabled = true;}}private void Button2_OnClick(object sender, RoutedEventArgs e){MessageBox.Show(textBox2.Text);if (ComboBox2.IsEnabled == false){ComboBox2.IsEnabled = true;}}
}

http://www.ppmy.cn/ops/158191.html

相关文章

23页PDF | 国标《GB/T 44109-2024 信息技术 大数据 数据治理实施指南 》发布

一、前言 《信息技术 大数据 数据治理实施指南》是中国国家标准化管理委员会发布的关于大数据环境下数据治理实施的指导性文件&#xff0c;旨在为组织开展数据治理工作提供系统性的方法和框架。报告详细阐述了数据治理的实施过程&#xff0c;包括规划、执行、评价和改进四个阶…

15 大 AWS 服务

在不断发展的云计算世界中&#xff0c;Amazon Web Services (AWS) 已成为一股主导力量&#xff0c;提供许多服务以满足各种应用程序开发、部署和管理方面的需求。本文将探讨 15 项 AWS 服务。这些服务对于构建可扩展、可靠且高效的系统至关重要。 1.Amazon EC2&#xff08;弹性…

2025年智慧化工园区整体解决方案-下载:安全生产管控,全生命周期数字管理架构

随着化工产业的快速发展&#xff0c;化工园区作为产业集聚的重要载体&#xff0c;面临着安全生产、环境保护、高效运营等诸多挑战。传统管理模式已难以满足现代化发展需求&#xff0c;智慧化工园区的建设成为必然趋势。 本文将介绍智慧化工园区的整体解决方案&#xff0c;重点…

JavaSE基本知识补充 -Map集合

目录 Map(key&#xff0c;value键值对呈现&#xff09; 1.1 Map的映射的特点 1. 2.HashMap &#xff08;键值对的业务偏多&#xff0c;而且hashmap在jdk1.7和1.8之间有所不同&#xff0c;性能做了提升&#xff0c;面试高频考点&#xff09; 1.3 Map接口的方法 方法 HashMap遍…

MySQL面试题合集

1.MySQL中的数据排序是怎么实现的? 回答重点 排序过程中,如果排序字段命中索引,则利用 索引排序。 反之,使用文件排序。 文件排序中,如果数据量少则在内存中排序, 具体是使用单路排序或者双路排序。 如果数据大则利用磁盘文件进行外部排序,一 般使用归并排序。 知识…

python视频爬虫

文章目录 爬虫的基本步骤一些工具模拟浏览器并监听文件视频爬取易错点一个代码示例参考 爬虫的基本步骤 1.抓包分析&#xff0c;利用浏览器的开发者工具 2.发送请求 3.获取数据 4.解析数据 5.保存数据 一些工具 requests, 用于发送请求&#xff0c;可以通过get&#xff0c;p…

【清晰教程】本地部署DeepSeek-r1模型

【清晰教程】通过Docker为本地DeepSeek-r1部署WebUI界面-CSDN博客 目录 Ollama 安装Ollama DeepSeek-r1模型 安装DeepSeek-r1模型 Ollama Ollama 是一个开源工具&#xff0c;专注于简化大型语言模型&#xff08;LLMs&#xff09;的本地部署和管理。它允许用户在本地计算机…

自制游戏——斗罗大陆

很简陋&#xff0c;没有图&#xff0c;请见谅 // mine[0] 级数 // mine[1] 战力 //mine[2] 1 白虎 //mine[2] 2 昊天锤 //mine[2] 3 蓝银草 #include <bits/stdc.h> using namespace std; int mine[100],live3, dou 1, luo 1, da 1, bag[1000], huan 0, lia…