DataGrid在自动生成列时在代码后面设置列的大小 [英] DataGrid Setting the size of columns in code behind when the columns are auto generated

查看:61
本文介绍了DataGrid在自动生成列时在代码后面设置列的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自动生成列

<DataGrid Grid.Row="0" AutoGenerateColumns="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Name="dataGrid1"   ItemsSource="{Binding Customers}" />

现在,我想在后面的代码中设置每列的大小,以执行以下操作

Now I want to set the size of each column in the code behind , to do something like this

<DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn Width="Auto" />
    <DataGridTextColumn Width="Auto" />
    <DataGridTextColumn Width="*" />
  </DataGrid.Columns>
</DataGrid>

我只想附加到每列 Auto和最后一个 *上,以及

I just want to attach to each column "Auto" , and to the last "*" , and everything in code behind.

谢谢。

推荐答案

尝试类似这

int lastColumn= dataGrid.Columns.Count -1;
dataGrid.Columns[lastColumn].Width = new DataGridLength(0, DataGridLengthUnitType.Star);

for(int i = 0 ; i < dataGrid.Columns.Count -1; i ++)
    dataGrid.Columns[i].Width = new DataGridLength(0, DataGridLengthUnitType.Auto);

当您使用 AutoGeneratingColumns 时,您可以处理 AutoGeneratedColumns 事件,并将代码放在该事件的上方

As you are AutoGeneratingColumns you can handle the AutoGeneratedColumns event of dataGrid and put code above inside that event

这篇关于DataGrid在自动生成列时在代码后面设置列的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆