WPF DataGrid 列宽和重新排序
我有一个 DataGrid 定义为
<wpftoolkit:DataGrid
x:Name="AccountsDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Accounts}"
ColumnReordered="DataGrid_ColumnReordered"
SelectionUnit="FullRow"
RowHeaderWidth="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
>
<wpftoolkit:DataGrid.Columns>
<wpftoolkit:DataGridTextColumn Header="Account Id" Binding="{Binding Path=AccountId}" Width="Auto" />
<wpftoolkit:DataGridTextColumn Header="Account Name" Binding="{Binding Path=AccountName}" Width="*" />
</wpftoolkit:DataGrid.Columns>
</wpftoolkit:DataGrid>
加载时看起来很棒。 第一列适合容纳内容和标题所需的最小宽度。 第二列延伸以填充 DataGrid 的其余宽度(因此我没有第三个填充列)。 但是,如果我尝试对列重新排序,则 AccountName 列的宽度无法调整为小于重新排序之前的宽度。 因此,我在 ColumnReordered 事件上添加了一个处理程序,认为我可以重置列宽度,但它似乎不起作用。 事实上,它将 AccountId 列缩小到几乎为零,并且 AccountName 列的大小仍然无法调整得更小。
private void DataGrid_ColumnReordered(object sender, Microsoft.Windows.Controls.DataGridColumnEventArgs e)
{
foreach (DataGridColumn column in AccountsDataGrid.Columns)
{
if (column.Equals(AccountsDataGrid.Columns.Last()))
{
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
}
else
{
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
}
}
}
有没有更好的方法来处理列宽以及为什么列重新排序会破坏列调整大小的能力
I have a DataGrid defined as
<wpftoolkit:DataGrid
x:Name="AccountsDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=Accounts}"
ColumnReordered="DataGrid_ColumnReordered"
SelectionUnit="FullRow"
RowHeaderWidth="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
>
<wpftoolkit:DataGrid.Columns>
<wpftoolkit:DataGridTextColumn Header="Account Id" Binding="{Binding Path=AccountId}" Width="Auto" />
<wpftoolkit:DataGridTextColumn Header="Account Name" Binding="{Binding Path=AccountName}" Width="*" />
</wpftoolkit:DataGrid.Columns>
</wpftoolkit:DataGrid>
which on load looks great. the first column fits to the minimum width necessary to fit both the content and the header. the second column stretches to fill the rest of the width of the DataGrid (so I don't have a 3rd filler column). But if I try to reorder the columns, the AccountName column cannot be resized to a width less than what its width was before the reorder. So I added a handler on the ColumnReordered event, figuring I can just reset the column widths, but it doesn't seem to work. In fact, it shrinks the AccountId column to almost nothing, and the AccountName column still cannot be resized smaller.
private void DataGrid_ColumnReordered(object sender, Microsoft.Windows.Controls.DataGridColumnEventArgs e)
{
foreach (DataGridColumn column in AccountsDataGrid.Columns)
{
if (column.Equals(AccountsDataGrid.Columns.Last()))
{
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Star);
}
else
{
column.Width = new DataGridLength(1.0, DataGridLengthUnitType.Auto);
}
}
}
Is there a better way to handle the column widths and why does the column reordering break the column resizing ability
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列在列集合中的位置没有改变。 只有 DisplayIndexes 发生变化。 尝试这个:
The columns are not changing position in the columns collection. Only the DisplayIndexes are changing. Try this: