zoukankan      html  css  js  c++  java
  • WPF DataGrid 对行中单元格的访问

    第一种方法:

    dataGridFirst.PreparingCellForEdit += new EventHandler<DataGridPreparingCellForEditEventArgs>(dataGridFirst_PreparingCellForEdit);
    
      void dataGridFirst_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
      {
      Suppose this list(get and set values)
      //var myValue = dataGrid1.SelectedItem[0].ToString(); 
      ClientsList selectedrow = ClientsList)dataGridFirst.SelectedItem;
      string Clientname = selectedrow.Name;
      int Clientid = selectedrow.ClientID;
      
      }

    第二种方法:

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) 
            {     
                DataGrid dataGrid = sender as DataGrid;  
                if (e.AddedItems!=null && e.AddedItems.Count>0)  
                {       
                    // find row for the first selected item     
                    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);    
                    if (row != null)   
                    {          
                        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);   
                        // find grid cell object for the cell with index 0       
                        DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;      
                        if (cell != null)     
                        {             
                            Console.WriteLine(((TextBlock)cell.Content).Text);         
                        }     
                    }   
                }
            }
    
    static T GetVisualChild<T>(Visual parent) where T : Visual
            { 
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
                for (int i = 0; i < numVisuals; i++) 
                { 
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T; 
                    if (child == null)
                        child = GetVisualChild<T>(v);
                    if (child != null)
                        break;
                }
                return child;
            }
    Top
    收藏
    关注
    评论
  • 相关阅读:
    浏览器内核
    为什么一般请求可以下载文件,Ajax 请求就不能下载
    转 深入理解javascript原型和闭包(10)——this
    node读取文本文件时,去掉BOM
    AMD & CMD
    gulp requirejs Error: ENOENT: no such file or directory, open '/js/require_config.js', 一直报找不到require_config.js,坑死了
    [BZOJ3224]普通平衡树
    [NOIP2014D2]
    [NOIP2014D1]
    [NOIP2013D2]
  • 原文地址:https://www.cnblogs.com/wangsu/p/WPF_DataGrid_Cell.html
Copyright © 2011-2022 走看看