1

.NET4.0 : コード ビハインドで DataGrid を構築しているため、XAML は使用していません。C# のみ。ユーザーが列ヘッダーの任意の場所を右クリックすると、コンテキスト メニューが表示されます。アイデアを提供するコードを次に示します。

    public void MakeAllColumns()
    {
        for (int i = 0; i < AllColumnDisplayNames.Length; i++)
        {
            // create a new column depending on the data to be displayed
            DataGridTextColumn col = new DataGridTextColumn();
            col.MaxWidth = 300;

            // create a new Textblock for column's header and add it to the column
            TextBlock headerText = new TextBlock() { Text = AllColumnDisplayNames[i] };
            col.Header = headerText;

            /// create a new context menu and add it to the header
            ContextMenu menu = new ContextMenu();
            headerText.ContextMenu = menu;

            // build the context menu depending on the property
            menu.Items.Add(new Button() { Content = "FOOBAR" });

            // add the bindings to the data
            col.Binding = new Binding(AllColumnBindings[i]);

            AllColumns.Add(AllColumnDisplayNames[i], col);
        }
    }

このアプローチの問題は、ユーザーがコンテキスト メニューをアクティブにするために、ヘッダーのどこかではなく、実際の TextBox をクリックする必要があることです。

TextBox をヘッダーの幅いっぱいにする方法が思い浮かばないので、TextBox の width プロパティを変更して列の幅にバインドすることしか考えられません。列はコンテンツに合わせて伸縮するため、幅が異なります。ただし、すべての列の ActualWidth プロパティをコンソールに出力すると、すべての列の幅が 20 であると表示されます。これは私の GUI の外観とは異なります。GUI での表示に対応する列幅を取得するにはどうすればよいですか?

4

1 に答える 1