我正在使用普通的数据网格列
我使用
获得价值
var item = datagrid1.SelectedItem;
var a = datagrid1.SelectedCells [0] .Column.GetCellContent(item)as TextBlock).Text
但是,如果我使用DataGridTextColumn而不是普通的datagrid文本列,则将值赋予“”;为什么?我怎样才能获得价值;
答案 0 :(得分:0)
你在用你的数据网格填充什么?我通常使用我创建的这个方法来获取数据上下文的值,而不是网格本身。
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个索引(列索引)的值,然后返回这些值以及这些值的索引,以防原始索引超出范围。