1

解決済み:

サブスクライブすることで解決しました-このメソッドが呼び出されると、ビジュアルツリーをトラバースして、操作する必要があるものをrow.Loaded見つけることができます。DataGridCellsPresenter

もちろん、これは理にかなっていますが、WPFをもっと理解することに投資する必要があります:(

元の質問:

DataGridCellsPresenter行が Datagrid に追加されたときに操作する必要があります。イベントにフックしLoadingRowて 経由でアクセスしようとしe.Rowましたが、イベントが発生したときに行がデータグリッドに挿入されていません (そのため、DataGridCellsPresenterビジュアルe.Rowツリーにe.Rowは存在せず、DataGrids 行にはまだありません)。

LoadedRow私の知る限り、イベントはないようです。ロードされたときに新しく追加された行にアクセスする方法はありますか?

PS。データグリッドと の両方でレイアウトを更新しようとしましたが、役に立ちe.Rowませんでした。

4

1 に答える 1

2

インデックスから行を取得できます。

    //found this on SO, I don't remember who, credit to original coder
    public static DataGridRow GetRow(this DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }

またはそれのデータ項目によって:

var row= DataGrid.ItemContainerGenerator.ContainerFromItem(youritem);

編集この方法もあなたを助けるかもしれません:

public static T FindChild<T>(DependencyObject parent, string childName)
                            where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's name is of the request name
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }
        return foundChild;
    }
于 2013-03-12T19:20:23.940 に答える