首页 > 其他分享 >WPF DataGrid控件扩展

WPF DataGrid控件扩展

时间:2023-09-11 09:34:37浏览次数:43  
标签:控件 cell System DataGrid dataGrid using WPF null row

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Controls.Helper
{
    public static class DataGridExtensions
    {
        /// <summary> 
        /// 返回给定行和列的DataGridCell
        /// </summary> 
        /// <param name="grid">The DataGrid</param> 
        /// <param name="row">The zero-based row index</param> 
        /// <param name="column">The zero-based column index</param> 
        /// <returns>The requested DataGridCell, or null if the indices are out of range</returns> 
        public static DataGridCell GetCell(this DataGrid grid, Int32 row, Int32 column)
        {
            DataGridRow gridrow = grid.GetRow(row);
            if (gridrow != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(gridrow);
                // try to get the cell but it may possibly be virtualized 
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell 
                    grid.ScrollIntoView(gridrow, grid.Columns[column]);

                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }

                return (cell);
            }

            return (null);
        }

        /// <summary> 
        /// 基于给定索引获取DataGridRow
        /// </summary> 
        /// <param name="idx">The zero-based index of the container to get</param> 
        public static DataGridRow GetRow(this DataGrid dataGrid, Int32 idx)
        {
            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(idx);
            if (row == null)
            {
                // may be virtualized, bring into view and try again 
                dataGrid.ScrollIntoView(dataGrid.Items[idx]);
                dataGrid.UpdateLayout();

                row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(idx);
            }

            return (row);
        }

        private static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);

            Int32 numvisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (Int32 i = 0; i < numvisuals; ++i)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                    child = GetVisualChild<T>(v);
                else
                    break;
            }

            return child;
        }
    }
}

  

标签:控件,cell,System,DataGrid,dataGrid,using,WPF,null,row
From: https://www.cnblogs.com/jnyyq/p/17692691.html

相关文章

  • WPF 用于查找控件的工具类:找到父控件、子控件
    WPF 封装查找控件的三个方法:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Media;namespaceK.Controls.Controls.Helper{///<summary>///用于查找控件的工具类:找到父......
  • 百度WebUploader开源上传控件
    前言首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已.下面先介绍一下WebUploader简介:WebUploader是由BaiduWebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件。在现代的浏览器里面能......
  • 【愚公系列】2023年09月 WPF控件专题 DockPanel控件详解
    (文章目录)前言WPF控件是WindowsPresentationFoundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见......
  • C# chart 控件操作
    Annotations:图表批注集合ChartAreas:添加成员ChartArea,轴AxesXaxisY(Value)axisTitle:轴标题StripLines:自定义条带和线条TextOreintation:文本方向Legends:(图表图例)if......
  • WPF 获取页面的子级
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Media; namespaceK.Controls.Controls.Helper{publicclassGetChildHelper{publicstaticTGetVisualCh......
  • WPF 获取资源文件帮助类
    usingSystem;usingSystem.Reflection;usingSystem.Windows;namespaceK.Controls.Controls.Helper{publicclassResourceHelper{privatestaticResourceDictionary_theme;internalstaticTGetResourceInternal<T>(stringkey)......
  • 表单引擎的自定义控件的概念与设计
    基本概念概述控件的定义:用于展示或者采集数据的表单元素,称为控件,比如:文本框、下拉框、单选按钮、从表等.自定义控件:表单引擎提供的基础控件之外的控件称为自定义控件,这些控件由开发人员自己定义,比如:评分、公文字号、定位等.基本控件分类字段控件:文本字段、数值字段、日期字段、......
  • WPF 使用Image实现动画旋转Loading
    首先需要有一个Loading的图片,(白色背景,白色小圆圈,所以显示看不到): 创建一个用户控件,实现动画的功能:<UserControlx:Class="K.Controls.Controls.LoadingImage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http:/......
  • WPF绑定(Binding)(4)
    数据绑定组件之间的绑定<StackPanel><Sliderx:Name="sd"Width="200"/><TextBoxText="{BindingElementName=sd,Path=Value}"/></StackPanel> 绑定数据源<Window.Resources>......
  • 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(1)
    在我们的SqlSugar的开发框架中,整合了Winform端、Vue3+ElementPlus的前端、以及基于UniApp+Vue+ThorUI的移动前端几个前端处理,基本上覆盖了我们日常的应用模式了,本篇随笔进一步介绍前端应用的领域,研究集成WPF的应用端,循序渐进介绍基于CommunityToolkit.Mvvm和HandyControl的WPF应......