【问题标题】:how to get selected item value of a DataGridHyperlinkColumn in wpf datagrid如何在wpf datagrid中获取DataGridHyperlinkColumn的选定项值
【发布时间】:2014-08-08 13:58:43
【问题描述】:

我正在使用普通的数据网格列,例如

我正在使用
获得价值 var item = datagrid1.SelectedItem; var a= datagrid1.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text

但是,如果我使用的是 DataGridTextColumn,则不是普通的 datagrid 文本列,而是将值作为“”;为什么?以及如何获得价值;

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    你用什么来填充你的数据网格?我通常使用我创建的这种方法从数据上下文而不是网格本身获取值。

    private Tuple<string,int,string,int> SelectedCell(System.Windows.Controls.DataGrid dataGrid, int textIndex, int valueIndex)
        {            
            string cellText = null, cellValue = null;
    
            //if the text index is out of bounds, fix it
            if (dataGrid.SelectedCells.Count < textIndex)
            {
                textIndex = dataGrid.SelectedCells.Count - 1;
            }
            else if (textIndex < 0)
            {
                textIndex = 0;
            }
    
            //if the value index is out of bounds, fix it
            if (dataGrid.SelectedCells.Count < valueIndex)
            {
                valueIndex = dataGrid.SelectedCells.Count - 1;
            }
            else if (valueIndex < 0)
            {
                valueIndex = 0;
            }
    
            System.Data.DataRowView row = dataGrid.SelectedItem as System.Data.DataRowView;
    
            if(row != null)
            {
                cellText = row[textIndex].ToString();
                cellValue = row[valueIndex].ToString();
            }
    
            return new Tuple<string,int,string,int>(cellText,textIndex, cellValue, valueIndex);
        }
    

    它获取 2 个索引(列索引)的值,然后返回这些值以及这些值的索引,以防原始索引超出范围。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 2013-10-14
      • 2021-03-31
      相关资源
      最近更新 更多