ExtensionHelper扩展帮助类
public static class ExtensionHelper{}
字符串转换成指定类型的值
using System.ComponentModel;
/// <summary>
/// 字符串转换成指定类型的值
/// </summary>
/// <typeparam name="T">指定类型</typeparam>
/// <param name="input">字符串</param>
/// <returns>类型的值</returns>
public static T Value<T>(this string input)
{
try
{
if (string.IsNullOrWhiteSpace(input) && typeof(T) != typeof(string))
{
return default;
}
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(input);
}
catch
{
// 转换异常会增加耗时
return default;
}
}
DataGrid扩展
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
var presenter = WSCommFunc.GetVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(row);
if (presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = WSCommFunc.GetVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(row);
}
return (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return null;
}
public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
DataGridRow rowContainer = grid.GetRow(row);
return grid.GetCell(rowContainer, column);
}
public static DataGridRow GetRow(this DataGrid grid, int index)
{
if (index < 0)
{
return null;
}
var row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// 可能已虚拟化,进入视图并重试
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
BindingList
/// <summary>
/// 添加指定集合到列表的结尾
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="collection"></param>
/// <remarks>只响应<see cref="ListChanged"/>一次</remarks>
public static void AddRange<T>(this BindingList<T> list, IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
var oldRaiseEventsValue = list.RaiseListChangedEvents;
try
{
list.RaiseListChangedEvents = false;
foreach (var item in collection)
{
list.Add(item);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
list.RaiseListChangedEvents = oldRaiseEventsValue;
if (list.RaiseListChangedEvents)
{
list.ResetBindings();
}
}
}
/// <summary>
/// 删除指定集合到列表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="collection"></param>
/// <remarks>只响应<see cref="ListChanged"/>一次</remarks>
public static void DeleteRange<T>(this BindingList<T> list, IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
var oldRaiseEventsValue = list.RaiseListChangedEvents;
try
{
list.RaiseListChangedEvents = false;
foreach (var item in collection)
{
list.Remove(item);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
list.RaiseListChangedEvents = oldRaiseEventsValue;
if (list.RaiseListChangedEvents)
{
list.ResetBindings();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」