English 中文(简体)
如何使XAML DataGridColumns填充整个DataGrid?
原标题:How do I make XAML DataGridColumns fill the entire DataGrid?

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

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

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

最佳回答

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

如果希望所有列平分格线,请将其应用于所有列。如果你只想用一个来填充剩余的空间,只需将其应用于该列,其余部分为“自动”或特定宽度。

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

问题回答

确保您的DataGrid的Width设置为类似{BindingPath=ActualWidth,RelativeSource={RelativeSsourceMode=FindAncestor,AncestorType=Window,AncesorLevel=1}的值

类似地,您在DataGrid.Column/DataGridXXXXColumn元素上设置Width=“*”属性应该可以工作。

如前所述,ColumnWidth=“*”非常适合XAML中的DataGrid。

我在这个上下文中使用了它:

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

我的2美分->;

聚会很晚

数据网格->;列->;宽度=“*”仅在DataGrid父容器具有固定宽度时有效。

示例:我将DataGrid放在Grid中->;数据网格中宽度=“Auto”然后宽度=“*”的列不起作用,但如果设置网格->;列宽=“450”表示固定,然后工作正常

将columns<code>Width</code>属性设置为比例宽度,例如<code>*

关于同一主题的另一个旋转:

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;
    }
 }

我添加了一个HorizontalAlignment=“Center”(默认值为“Strech”),它解决了我的问题,因为它只使数据网格根据需要而宽。(删除了数据网格的宽度设置(如果您有)。)

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

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

对于那些正在寻找C#解决方案的人:

如果出于某种原因需要启用“自动生成列”,可以做的一件事是指定除要自动调整大小的列之外的所有列的宽度(它将不占用剩余空间,但会根据单元格的内容调整大小)。

示例(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; 

对我来说,这是一种变通方法,因为当用户最大化窗口时,我需要调整DataGrid的大小。

对我来说效果很好,只需将columnbwidth依赖属性设置为columnbwidth=“*”它将像winforms一样将列宽填充为数据网格宽度(autosize=fill)

 <DataGrid Grid.Row="0" x:Name="dg1" VerticalAlignment="Top" AutoGenerateColumns="False" Margin="0,0,-6,0" Width="1520" Height="700"  CanUserAddRows="True" 
        
        CanUserDeleteRows="True" ItemsSource="{Binding EmployeeData}" RowDetailsVisibilityMode="Visible" HorizontalGridLinesBrush="DarkBlue" VerticalGridLinesBrush="DarkGreen" ColumnWidth="*" >




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签