You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
4.2 KiB
C#
114 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.ComponentModel;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace AI.Models.SpecialMessages
|
|
{
|
|
/// <summary>
|
|
/// 表格数据预览行 - 一行单元格
|
|
/// </summary>
|
|
public class TableDataRow : INotifyPropertyChanged
|
|
{
|
|
/// <summary>该行单元格(与列顺序一致)</summary>
|
|
public ObservableCollection<string> Cells { get; } = new ObservableCollection<string>();
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
protected void OnPropertyChanged(string propertyName) =>
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 表格数据展示特殊消息 - 用于导入数据等在聊天流中做数据预览
|
|
/// </summary>
|
|
public class TableDataMessage : ISpecialMessage, INotifyPropertyChanged
|
|
{
|
|
private string _title = "数据预览";
|
|
private int _totalRowCount;
|
|
private int _maxPreviewRows = 50;
|
|
|
|
/// <summary>消息唯一标识符</summary>
|
|
public string Id { get; set; } = Guid.NewGuid().ToString();
|
|
|
|
/// <summary>类型名称</summary>
|
|
public string TypeName => "Table";
|
|
|
|
/// <summary>不需要实时更新</summary>
|
|
public bool IsLive => false;
|
|
|
|
/// <summary>标题(如“数据预览”)</summary>
|
|
public string Title
|
|
{
|
|
get => _title;
|
|
set => SetProperty(ref _title, value);
|
|
}
|
|
|
|
/// <summary>列名(表头)</summary>
|
|
public ObservableCollection<string> ColumnNames { get; } = new ObservableCollection<string>();
|
|
|
|
/// <summary>预览行数据</summary>
|
|
public ObservableCollection<TableDataRow> Rows { get; } = new ObservableCollection<TableDataRow>();
|
|
|
|
/// <summary>数据总行数(用于显示“共 N 行,仅显示前 M 行”)</summary>
|
|
public int TotalRowCount
|
|
{
|
|
get => _totalRowCount;
|
|
set => SetProperty(ref _totalRowCount, value);
|
|
}
|
|
|
|
/// <summary>最多预览行数</summary>
|
|
public int MaxPreviewRows
|
|
{
|
|
get => _maxPreviewRows;
|
|
set => SetProperty(ref _maxPreviewRows, value);
|
|
}
|
|
|
|
/// <summary>是否有多余行未显示</summary>
|
|
public bool HasMoreRows => TotalRowCount > Rows.Count;
|
|
|
|
/// <summary>摘要文案(如“共 100 行,仅显示前 50 行”)</summary>
|
|
public string SummaryText => $"共 {TotalRowCount} 行,仅显示前 {Rows.Count} 行";
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
|
|
{
|
|
if (EqualityComparer<T>.Default.Equals(field, value))
|
|
return false;
|
|
field = value;
|
|
OnPropertyChanged(propertyName);
|
|
if (propertyName == nameof(TotalRowCount) || propertyName == nameof(MaxPreviewRows))
|
|
{
|
|
OnPropertyChanged(nameof(HasMoreRows));
|
|
OnPropertyChanged(nameof(SummaryText));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>从列名与行数据批量填充(每行为字符串数组,与列顺序一致)</summary>
|
|
public void SetData(IEnumerable<string> columnNames, IEnumerable<IEnumerable<string>> rows, int? totalRowCount = null)
|
|
{
|
|
ColumnNames.Clear();
|
|
foreach (var c in columnNames)
|
|
ColumnNames.Add(c ?? string.Empty);
|
|
|
|
Rows.Clear();
|
|
foreach (var row in rows)
|
|
{
|
|
var r = new TableDataRow();
|
|
foreach (var cell in row)
|
|
r.Cells.Add(cell?.ToString() ?? string.Empty);
|
|
Rows.Add(r);
|
|
}
|
|
|
|
TotalRowCount = totalRowCount ?? Rows.Count;
|
|
OnPropertyChanged(nameof(HasMoreRows));
|
|
OnPropertyChanged(nameof(SummaryText));
|
|
}
|
|
}
|
|
}
|