WPF 数据表格:点击按钮增删行,可读可写
以下是 WPF 代码实现:
XAML 代码:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Add Row" Click="AddRow_Click"/>
<Button Grid.Row="2" Content="Delete Last Row" Click="DeleteRow_Click"/>
<DataGrid Grid.Row="1" Name="dataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="光源1" Binding="{Binding LightSource}" />
<DataGridTextColumn Header="电流" Binding="{Binding Current}" />
<DataGridTextColumn Header="电压" Binding="{Binding Voltage}" />
<DataGridTextColumn Header="泵异常温度值" Binding="{Binding PumpAbnormalTemperature}" />
<DataGridTextColumn Header="泵温度值" Binding="{Binding PumpTemperature}" />
<DataGridTextColumn Header="功率异常值" Binding="{Binding PowerAbnormalValue}" />
<DataGridTextColumn Header="功率值" Binding="{Binding PowerValue}" />
<DataGridCheckBoxColumn Header="开/关" Binding="{Binding IsOn}" />
<DataGridTextColumn Header="标示图字段" Binding="{Binding Image}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
C# 代码:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
namespace WpfApp1
{
public partial class MainWindow : Window
{
public ObservableCollection<DataRow> Rows { get; set; }
public MainWindow()
{
InitializeComponent();
Rows = new ObservableCollection<DataRow>();
dataGrid.ItemsSource = Rows;
}
private void AddRow_Click(object sender, RoutedEventArgs e)
{
Rows.Add(new DataRow());
}
private void DeleteRow_Click(object sender, RoutedEventArgs e)
{
if (Rows.Count > 0)
{
Rows.RemoveAt(Rows.Count - 1);
}
}
}
public class DataRow : INotifyPropertyChanged
{
private string _lightSource;
public string LightSource
{
get => _lightSource;
set
{
_lightSource = value;
OnPropertyChanged(nameof(LightSource));
}
}
private double _current;
public double Current
{
get => _current;
set
{
_current = value;
OnPropertyChanged(nameof(Current));
}
}
private double _voltage;
public double Voltage
{
get => _voltage;
set
{
_voltage = value;
OnPropertyChanged(nameof(Voltage));
}
}
private double _pumpAbnormalTemperature;
public double PumpAbnormalTemperature
{
get => _pumpAbnormalTemperature;
set
{
_pumpAbnormalTemperature = value;
OnPropertyChanged(nameof(PumpAbnormalTemperature));
}
}
private double _pumpTemperature;
public double PumpTemperature
{
get => _pumpTemperature;
set
{
_pumpTemperature = value;
OnPropertyChanged(nameof(PumpTemperature));
}
}
private double _powerAbnormalValue;
public double PowerAbnormalValue
{
get => _powerAbnormalValue;
set
{
_powerAbnormalValue = value;
OnPropertyChanged(nameof(PowerAbnormalValue));
}
}
private double _powerValue;
public double PowerValue
{
get => _powerValue;
set
{
_powerValue = value;
OnPropertyChanged(nameof(PowerValue));
}
}
private bool _isOn;
public bool IsOn
{
get => _isOn;
set
{
_isOn = value;
OnPropertyChanged(nameof(IsOn));
}
}
private string _image;
public string Image
{
get => _image;
set
{
_image = value;
OnPropertyChanged(nameof(Image));
}
}
public DataRow()
{
// 设置默认值
LightSource = "光源1";
Current = 0;
Voltage = 0;
PumpAbnormalTemperature = 0;
PumpTemperature = 0;
PowerAbnormalValue = 0;
PowerValue = 0;
IsOn = false;
Image = "";
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
效果如下图所示:

原文地址: https://www.cveoy.top/t/topic/nwDf 著作权归作者所有。请勿转载和采集!