C#WPF如何在代码后面按比例调整datagrid列

时间:2015-12-01 09:49:33

标签: c# wpf code-behind datagridcolumn

我有一个datagrid,其列属性必须以编程方式在代码后面更改为此我使用事件

AutoGeneratingColumn="Datagrid_AutoGeneratingColumn"

我做的事情:

 private void Datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
  string strDatagridName = (sender as DataGrid).Name;
  switch (strDatagridName.ToUpper())
  {
    case "DTGPPTAB1":
    case "DTGPPTAB3":
      switch (e.PropertyName.ToUpper())
      {
        /*----Visible ----*/
        case "DESCRIPTION": e.Column.Header = Langs.Word(Langs.eWords.Description); e.Column.Width = 33; break;<-------------here
        case "NOTE": e.Column.Header = Langs.Word(Langs.eWords.Notes);  break;
        case "PARTPROGRAMFILENAME": e.Column.Header = Langs.Word(Langs.eWords.PPfilename);  break;

          /*----Hidden ----*/
        case "LSTBLADES": e.Column.Header = Langs.Word(Langs.eWords.Blades); break;
        case "LSTMYIMAGES": e.Column.Header = Langs.Word(Langs.eWords.Image); e.Column.Visibility = Visibility.Collapsed;  break;
        case "LSTMYSOUNDFILES": e.Column.Header = Langs.Word(Langs.eWords.Sound); e.Column.Visibility = Visibility.Collapsed; break;
        case "BARCODE": e.Column.Header = Langs.Word(Langs.eWords.Barcode); e.Column.Visibility = Visibility.Collapsed; break;
        case "PROGRAMTRACEFIELDS": e.Column.Header = Langs.Word(Langs.eWords.ProgramTracefields); e.Column.Visibility = Visibility.Collapsed; break;
        default: e.Column.Header = e.PropertyName; break;
      }
      break;
  }

一切正常。 我需要的是描述案例。在这里,我可以设置以像素为单位的列,但我不能按比例设置它,例如33%。

所以简而言之,我想设置前3列,将整个数据网格空间划分为33%33%33%。所以在%而不是像素。 感谢名单

1 个答案:

答案 0 :(得分:3)

您需要将Width设置为double,而不是将DataGridLength设置为e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star); 。这是一个例子:

1*

将这些列设置为{{1}}应足以公平地分配宽度(3列为33%)。