首页 新闻 会员 周边 捐助

wpf 获取datagrid 模板列中的控件

3
悬赏园豆:10 [已解决问题] 解决于 2012-09-17 22:40

目前采用的 方法  (网上提供的一款)

 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]);

诸位有好的解决方法吗 最好 带实例 谢谢

不起名字了的主页 不起名字了 | 初学一级 | 园豆:25
提问于:2012-09-04 17:33
< >
分享
最佳答案
1
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>
收获园豆:10
答应不爱你 | 初学一级 |园豆:3 | 2012-09-06 14:38

您提供的方法,跟我现在用的一样,然后 会出现 FrameworkElement 为空的情况。请问有什么其他方式吗。?

您这种方式我已经用过了。

不起名字了 | 园豆:25 (初学一级) | 2012-09-10 09:56

@不起名字了: 我就是用的这种方法,很正确的呀

答应不爱你 | 园豆:3 (初学一级) | 2012-09-11 13:05

@答应不爱你:  我这边有这样一个现象,有多行数据,其中大部分数据正常,某些行数据的时候

获取myDataGrid.Columns[columnIndex].GetCellContent(myDataGrid.Items[rowIndex]); 失败

为NULL 请问这是什么原因,麻烦您了帮忙指点一下。

不起名字了 | 园豆:25 (初学一级) | 2012-09-11 18:23

@不起名字了: 你是唯一一个回答的 ,分数给你吧 。具体 原因是因为 页面没有看到这个加载项 (因为我的列表没有使用分页 所以出现 次问题 )如果以后出现此类问题 ,所有行显示 即可

不起名字了 | 园豆:25 (初学一级) | 2012-09-17 22:41

牛逼啊 完美解决了这个问题

狂扁小朋友 | 园豆:10 (初学一级) | 2015-12-27 20:30

@不起名字了: 请问这个问题你是怎么解决的啊,我也出现这个问题了

真爱生命 | 园豆:200 (初学一级) | 2015-12-31 16:38
其他回答(1)
1

你的datagrid是默认开启了ui virtualization 的吧,如果是这样的话,VisualTree并不是所有的控件,为了显示加速,virtualization默认的只会加载一定范围的控件,不显示的控件并不加载

猿人甲 | 园豆:208 (菜鸟二级) | 2013-04-11 20:17

正解!!!!!!!!!!!!! 怒赞!!!!!!!!!!!!!!!!!!!

在datagrid属性里面要关闭就可以了,不过加时间是问题,如果不是分页的话

EnableColumnVirtualization="False"                                                                  EnableRowVirtualization="False"

支持(0) 反对(0) sunxiaotianmg | 园豆:200 (初学一级) | 2013-08-12 17:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册