简体   繁体   中英

WPF: binding based on column index

In WPF, how can I create a data trigger based on a DataGrid cell's column index? I can get at the column, but the column only has the DisplayIndex available, not the real index. I need to style the cell in a way that includes the colum as one of multiple trigger conditions, so I cannot just define a style for the column.

Assuming you are working with adding columns of type DataGridTextColumn, you could do something like this:

public class MyDataGridTextColumn :DataGridTextColumn 
{
   public int ColumnIndex {get;private set;}

   public MyDataGridTextColumn (int columnIndex)
   {
       ColumnIndex = columnIndex;
   }
}

So by providing the column index (your value you need to find later) at the point in time of constructing the column, you can then access that when you are styling the column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM