目前采用的 方法 (网上提供的一款)
public static DataGridRow GetRow(DataGrid datagrid, int columnIndex)
{
DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex);
if (row == null)
{
datagrid.UpdateLayout();
datagrid.ScrollIntoView(datagrid.Items[columnIndex]);
row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return row;
}
public 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;
}
public static DataGridCell GetCell(DataGrid datagrid, int rowIndex, int columnIndex)
{
DataGridCell cell=new DataGridCell ();
try
{
DataGridRow rowContainer = GetRow(datagrid, rowIndex);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
//cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
cell = CommonHelper.GetCell(datagrid, rowIndex, 8);
if (cell == null)
{
datagrid.ScrollIntoView(rowContainer, datagrid.Columns[columnIndex]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
}
return cell;
}
}
catch (Exception ex)
{
}
return cell;
}
public static T FindVisualChildByName<T>(Visual parent, string name) where T : Visual
{
if (parent != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i) as Visual;
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
{
return child as T;
}
else
{
T result = FindVisualChildByName<T>(child, name);
if (result != null)
return result;
}
}
}
return null;
}
自己使用的一种方法
string strReportStatus = (dgReportList.Items[i] as DataRowView)["STATUS"].ToString();
DataGridTemplateColumn templeColumn = dgReportList.Columns[8] as DataGridTemplateColumn;
FrameworkElement s = dgReportList.Columns[8].GetCellContent(dgReportList.Items[i]);
TextBlock tbOper = templeColumn.CellTemplate.FindName("blockOper", s) as TextBlock;
当循环到一定的 行时 会报出为空 FrameworkElement s = dgReportList.Columns[8].GetCellContent(dgReportList.Items[i]);
诸位有好的解决方法吗 最好 带实例 谢谢
C# 下面是获取末班中的控件textbox
public object FindName(System.Windows.Controls.DataGrid myDataGrid, int columnIndex, int rowIndex, string controlName) { FrameworkElement item = myDataGrid.Columns[columnIndex].GetCellContent(myDataGrid.Items[rowIndex]); DataGridTemplateColumn temp = (myDataGrid.Columns[columnIndex] as DataGridTemplateColumn); return temp.CellTemplate.FindName(controlName, item); }
for (int i = 0; i < dtDefiStan.Rows.Count; i++) { txtPoint = FindName(dataGr1, 4, i, "textbox1_point") as System.Windows.Controls.TextBox; }
<DataGrid AutoGenerateColumns="False" Height="732" Name="dataGr1" VerticalAlignment="Top" Margin="371.167,179,26.833,0" RowHeaderWidth="0" CanUserAddRows="False" FontFamily="微软雅黑" FontSize="12" Background="White"> <DataGrid.Columns> <DataGridTemplateColumn x:Name="dataGrid_stu1" Header='' Width="162"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="+" HorizontalAlignment="Left" Width="35" Height="37.333" VerticalAlignment="Top" Margin="0 15 0 0" > <TextBox Padding="0 25 0 0" Width="80" MaxLength="2" Background="Transparent" BorderThickness="0" TextAlignment="Center" Name="textbox1_point"></TextBox> <Button Content="-" HorizontalAlignment="Left" Width="35" Height="37.333" VerticalAlignment="Top" Margin="0 15 0 0"/> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn x:Name="dataGrid_stu2" Header='' Width="162"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="+" HorizontalAlignment="Left"Width="35" Height="37.333" VerticalAlignment="Top" Margin="0 15 0 0" /> <TextBox Padding="0 25 0 0" Width="80" MaxLength="2" Background="Transparent" BorderThickness="0" TextAlignment="Center" Name="textbox2_point" /> <Button Content="-" HorizontalAlignment="Left" Width="35" Height="37.333" VerticalAlignment="Top" Margin="0 15 0 0" /> </StackPanel> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
您提供的方法,跟我现在用的一样,然后 会出现 FrameworkElement 为空的情况。请问有什么其他方式吗。?
您这种方式我已经用过了。
@不起名字了: 我就是用的这种方法,很正确的呀
@答应不爱你: 我这边有这样一个现象,有多行数据,其中大部分数据正常,某些行数据的时候
获取myDataGrid.Columns[columnIndex].GetCellContent(myDataGrid.Items[rowIndex]); 失败
为NULL 请问这是什么原因,麻烦您了帮忙指点一下。
@不起名字了: 你是唯一一个回答的 ,分数给你吧 。具体 原因是因为 页面没有看到这个加载项 (因为我的列表没有使用分页 所以出现 次问题 )如果以后出现此类问题 ,所有行显示 即可
牛逼啊 完美解决了这个问题
@不起名字了: 请问这个问题你是怎么解决的啊,我也出现这个问题了
你的datagrid是默认开启了ui virtualization 的吧,如果是这样的话,VisualTree并不是所有的控件,为了显示加速,virtualization默认的只会加载一定范围的控件,不显示的控件并不加载
正解!!!!!!!!!!!!! 怒赞!!!!!!!!!!!!!!!!!!!
在datagrid属性里面要关闭就可以了,不过加时间是问题,如果不是分页的话
EnableColumnVirtualization="False" EnableRowVirtualization="False"