gpt4 book ai didi

c# - Silverlight DataGrid 如何从选定项中获取单元格值?

转载 作者:太空狗 更新时间:2023-10-30 00:58:18 26 4
gpt4 key购买 nike

我正在尝试从 silverlight 数据网格的选定项中获取单元格值。在附加的代码中,我可以获取单元格的属性并更改其前景色,但我无法获取单元格的值。有人可以让我知道我做错了什么吗?非常感谢您的帮助!

    private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;

int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;

DataGridColumn column = dataGrid.Columns[0];
FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));

if (result != null)
{
DataGridCell cell = (DataGridCell)result;
//changes the forecolor
cell.Foreground = new SolidColorBrush(Colors.Blue);
//how to get cell value?
}
}
}

private FrameworkElement GetParent(FrameworkElement child, Type targetType)
{
object parent = child.Parent;
if (parent != null)
{
if (parent.GetType() == targetType)
{
return (FrameworkElement)parent;
}
else
{
return GetParent((FrameworkElement)parent, targetType);
}
}
return null;
}

最佳答案

感谢 VooDooChild,请参阅下文了解我使用文本 block 获取值(value)的解决方案。

private void FindDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;

int selectedIndex = dataGrid.SelectedIndex;
if (selectedIndex > -1)
{
FindResult findResult = (FindResult)FindDetailsDataGrid.SelectedItem;

DataGridColumn column = dataGrid.Columns[0];
FrameworkElement fe = column.GetCellContent(dataGrid.SelectedItem);
FrameworkElement result = GetParent(fe, typeof(DataGridCell));

if (result != null)
{
DataGridCell cell = (DataGridCell)result;
//changes the forecolor
cell.Foreground = new SolidColorBrush(Colors.Blue);
//how to get cell value?

TextBlock block = fe as TextBlock;
if (block != null)
{
string cellText = block.Text;
MessageBox.Show(cellText);
}
}
}
}

关于c# - Silverlight DataGrid 如何从选定项中获取单元格值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3120952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com