如何让XAML DataGridColumns填充整个DataGrid?

时间:2022-01-19 22:37:00

I am using DataGrids in XAML (not Silverlight) with resizable columns, the DataGrid will expand if the user resizes the screen.

我在XAML中使用DataGrid(不是Silverlight)和可调整大小的列,如果用户调整屏幕的大小,DataGrid将会展开。

Currently if the widths of all the DataGrid columns are less than the width of the DataGrid I get an extra "column" appearing which is unclickable and serves no purpose.

目前,如果所有DataGrid列的宽度都小于DataGrid的宽度,我将得到一个额外的“列”,它是不可单击的,没有任何用途。

Does anyone know how to make one column always resize to fill all the remaining space?

有人知道如何使一列始终调整大小以填充所有剩余的空间吗?

9 个解决方案

#1


204  

If you use Width="*" the column will fill to expand the available space.

如果使用Width="*",则该列将填充以展开可用空间。

If you want all columns to divide the grid equally apply this to all columns. If you just want one to fill the remaining space just apply it to that column with the rest being "Auto" or a specific width.

如果你想让所有的列分开网格,把这个应用到所有的列上。如果你只是想让其中一个填充剩余的空间,只需将它应用到该列中,其余部分是“自动的”或特定的宽度。

You can also use Width="0.25*" (for example) if you want the column to take up 1/4 of the available width.

如果希望列占可用宽度的1/4,也可以使用Width="0.25*"(例如)。

#2


11  

Make sure your DataGrid has Width set to something like {Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window,AncestorLevel=1}}.

确保您的DataGrid的宽度设置为{Binding Path=ActualWidth, RelativeSource= FindAncestor,AncestorType=Window,AncestorLevel=1}。

Like that, your setting of Width="*" attribute on DataGrid.Columns/DataGridXXXXColumn elements should work.

这样,在DataGrid上设置Width="*"属性。列/ DataGridXXXXColumn元素应该工作。

#3


5  

Set the columns Width property to be a proportional width such as *

将列宽度属性设置为比例宽度,如*

#4


2  

As noted, the ColumnWidth="*" worked perfectly well for a DataGrid in XAML.

如前所述,对于XAML中的DataGrid, ColumnWidth=“*”非常有效。

I used it in this context:

我把它用在这方面:

    <DataGrid ItemsSource="{Binding AllFolders, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ColumnWidth="*"/>

#5


1  

I added a HorizontalAlignment="Center" (The default is "Strech") and it solved my problem because it made the datagrid only as wide as needed. (Removed the datagrid's Width setting if you have one.)

我添加了一个horizontalalign ="Center"(默认值是"Strech"),它解决了我的问题,因为它只使datagrid尽可能宽。(如果有的话,删除datagrid的宽度设置。)

如何让XAML DataGridColumns填充整个DataGrid?

#6


1  

Another spin on the same theme:

关于同一主题的另一种说法是:

protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
    {
        dataGrid.Width = e.NewSize.Width - (e.NewSize.Width * .1);

        foreach (var column in dataGrid.Columns)
        {
            column.Width = dataGrid.Width / dataGrid.Columns.Count;
        }

    }

#7


0  

This will not expand the last column of the xaml grid to take the remaining space if AutoGeneratedColumns="True".

如果AutoGeneratedColumns=“True”,则不会展开xaml网格的最后一列以获取剩余的空间。

#8


0  

set ONE column's width to any value, i.e. width="*"

将一列的宽度设置为任意值,即宽度="*"

#9


0  

For those looking for a C# workaround:

对于那些寻找c#解决方案的人:

If you need for some reason to have the "AutoGeneratedColumns" enabled, one thing you can do is to specify all the columns's width except the ones you want to be auto resized (it will not take the remaining space, but it will resize to the cell's content).

如果您需要一些理由来启用“自动生成列”,那么您可以做的一件事是指定所有列的宽度,除非您想要自动调整大小(它不会占用剩余的空间,但是它会调整到单元格的内容)。

Example (dgShopppingCart is my DataGrid):

示例(dgShopppingCart是我的DataGrid):

dgShoppingCart.Columns[0].Visibility = Visibility.Hidden; 
dgShoppingCart.Columns[1].Header = "Qty";
dgShoppingCart.Columns[1].Width = 100;
dgShoppingCart.Columns[2].Header = "Product Name"; /*This will be resized to cell content*/
dgShoppingCart.Columns[3].Header = "Price";
dgShoppingCart.Columns[3].Width = 100;
dgShoppingCart.Columns[4].Visibility = Visibility.Hidden; 

For me it works as a workaround because I needed to have the DataGrid resized when the user maximize the Window.

对我来说,它可以作为一个解决方案,因为当用户最大化窗口时,我需要调整DataGrid的大小。

#1


204  

If you use Width="*" the column will fill to expand the available space.

如果使用Width="*",则该列将填充以展开可用空间。

If you want all columns to divide the grid equally apply this to all columns. If you just want one to fill the remaining space just apply it to that column with the rest being "Auto" or a specific width.

如果你想让所有的列分开网格,把这个应用到所有的列上。如果你只是想让其中一个填充剩余的空间,只需将它应用到该列中,其余部分是“自动的”或特定的宽度。

You can also use Width="0.25*" (for example) if you want the column to take up 1/4 of the available width.

如果希望列占可用宽度的1/4,也可以使用Width="0.25*"(例如)。

#2


11  

Make sure your DataGrid has Width set to something like {Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window,AncestorLevel=1}}.

确保您的DataGrid的宽度设置为{Binding Path=ActualWidth, RelativeSource= FindAncestor,AncestorType=Window,AncestorLevel=1}。

Like that, your setting of Width="*" attribute on DataGrid.Columns/DataGridXXXXColumn elements should work.

这样,在DataGrid上设置Width="*"属性。列/ DataGridXXXXColumn元素应该工作。

#3


5  

Set the columns Width property to be a proportional width such as *

将列宽度属性设置为比例宽度,如*

#4


2  

As noted, the ColumnWidth="*" worked perfectly well for a DataGrid in XAML.

如前所述,对于XAML中的DataGrid, ColumnWidth=“*”非常有效。

I used it in this context:

我把它用在这方面:

    <DataGrid ItemsSource="{Binding AllFolders, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ColumnWidth="*"/>

#5


1  

I added a HorizontalAlignment="Center" (The default is "Strech") and it solved my problem because it made the datagrid only as wide as needed. (Removed the datagrid's Width setting if you have one.)

我添加了一个horizontalalign ="Center"(默认值是"Strech"),它解决了我的问题,因为它只使datagrid尽可能宽。(如果有的话,删除datagrid的宽度设置。)

如何让XAML DataGridColumns填充整个DataGrid?

#6


1  

Another spin on the same theme:

关于同一主题的另一种说法是:

protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
    {
        dataGrid.Width = e.NewSize.Width - (e.NewSize.Width * .1);

        foreach (var column in dataGrid.Columns)
        {
            column.Width = dataGrid.Width / dataGrid.Columns.Count;
        }

    }

#7


0  

This will not expand the last column of the xaml grid to take the remaining space if AutoGeneratedColumns="True".

如果AutoGeneratedColumns=“True”,则不会展开xaml网格的最后一列以获取剩余的空间。

#8


0  

set ONE column's width to any value, i.e. width="*"

将一列的宽度设置为任意值,即宽度="*"

#9


0  

For those looking for a C# workaround:

对于那些寻找c#解决方案的人:

If you need for some reason to have the "AutoGeneratedColumns" enabled, one thing you can do is to specify all the columns's width except the ones you want to be auto resized (it will not take the remaining space, but it will resize to the cell's content).

如果您需要一些理由来启用“自动生成列”,那么您可以做的一件事是指定所有列的宽度,除非您想要自动调整大小(它不会占用剩余的空间,但是它会调整到单元格的内容)。

Example (dgShopppingCart is my DataGrid):

示例(dgShopppingCart是我的DataGrid):

dgShoppingCart.Columns[0].Visibility = Visibility.Hidden; 
dgShoppingCart.Columns[1].Header = "Qty";
dgShoppingCart.Columns[1].Width = 100;
dgShoppingCart.Columns[2].Header = "Product Name"; /*This will be resized to cell content*/
dgShoppingCart.Columns[3].Header = "Price";
dgShoppingCart.Columns[3].Width = 100;
dgShoppingCart.Columns[4].Visibility = Visibility.Hidden; 

For me it works as a workaround because I needed to have the DataGrid resized when the user maximize the Window.

对我来说,它可以作为一个解决方案,因为当用户最大化窗口时,我需要调整DataGrid的大小。