Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-DataGridColumn Width未返回预期值_C#_Wpf_Wpfdatagrid_Datagridcolumn_Datagridcolumnheader - Fatal编程技术网

C# WPF-DataGridColumn Width未返回预期值

C# WPF-DataGridColumn Width未返回预期值,c#,wpf,wpfdatagrid,datagridcolumn,datagridcolumnheader,C#,Wpf,Wpfdatagrid,Datagridcolumn,Datagridcolumnheader,.NET4.0:我正在代码隐藏中构建一个数据网格,所以我没有使用任何XAML。只有C。当用户右键单击列标题中的任何位置时,我想显示一个上下文菜单。下面是一些代码,可以让您了解: public void MakeAllColumns() { for (int i = 0; i < AllColumnDisplayNames.Length; i++) { // create a new column depending o

.NET4.0:我正在代码隐藏中构建一个数据网格,所以我没有使用任何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);
        }
    }
public void MakeAllColumns()
{
for(int i=0;i
这种方法的问题是,用户需要单击实际的文本框来激活上下文菜单,而不是在标题上的任何位置


由于我想不出让TextBox填充标题宽度的方法,所以我所能做的就是更改TextBox width属性以绑定到列的宽度。列拉伸以适合其内容,因此它们具有不同的宽度。然而,当我将所有列ActualWidth属性打印到console时,它表示它们都有宽度20,这不是我的GUI看起来的样子。如何获得与我的GUI中的显示方式相对应的列宽?

要解决您的问题,必须通过此正文交换您的正文方法:

此代码经过测试:

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 context menu 
                    ContextMenu menu = new ContextMenu();

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

                    // create a new column's header and add it to the column
                    DataGridColumnHeader head = new DataGridColumnHeader() { Content = AllColumnBindings[i] };
                    head.ContextMenu = menu;//add context menu to DataGridColumnHeader
                    col.Header = head;

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

                    AllColumns.Add(AllColumnDisplayNames[i], col);
                }
for(int i=0;i

我没有使用
TextBlock
而是使用了
DataGridColumnHeader
,它具有
ContextMenu
属性,因此占用了
标题的所有空间(高度和宽度)
要解决问题,必须通过此主体交换您的主体方法:

此代码经过测试:

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 context menu 
                    ContextMenu menu = new ContextMenu();

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

                    // create a new column's header and add it to the column
                    DataGridColumnHeader head = new DataGridColumnHeader() { Content = AllColumnBindings[i] };
                    head.ContextMenu = menu;//add context menu to DataGridColumnHeader
                    col.Header = head;

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

                    AllColumns.Add(AllColumnDisplayNames[i], col);
                }
for(int i=0;i
我没有使用
TextBlock
而是使用了
DataGridColumnHeader
,它具有
ContextMenu
属性,因此占据了
标题的所有空间(高度和宽度)