我有一个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%。所以在%而不是像素。 感谢名单
答案 0 :(得分:3)
您需要将Width
设置为double
,而不是将DataGridLength
设置为e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
。这是一个例子:
1*
将这些列设置为{{1}}应足以公平地分配宽度(3列为33%)。