wpf datagrid 在 xaml 中前两个单元格或列索引大于 1 之后应用单元格模板

wpf datagrid apply cell template after first two cells or column index greater than 1 in xaml

我创建了单元格模板,在单元格模板中添加了数据类型为 double 的数据模板。我想在数据网格的前两列之后应用单元格模板或数据模板

怎么做

您可以根据某些条件使用 DataGridTemplateColumn.CellTemplateSelector 到 select 模板..

快速搜索找到了这个 link

看看对你有没有好处。

 for (int i = 0; i < gridHolesList.Columns.Count; i++)
{
    if (gridHolesList.Columns[i].FieldName == "#" || gridHolesList.Columns[i].FieldName == "Name")
    {
        continue;
    }
    string cellTemplate = @"
    <DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                 xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
                  xmlns:dxe=""http://schemas.devexpress.com/winfx/2008/xaml/editors"">
       <dxe:TextEdit x:Name=""PART_Editor""
                   EditValue=""{Binding RelativeSource={RelativeSource Self},Mode=OneWayToSource , Path=EditValue,
                                                                          Converter={StaticResource FeetInchesInputConverter} , 
                                                                          UpdateSourceTrigger=PropertyChanged}""
                     Style=""{StaticResource FeetInchesRegualarExpression}"" 
  />

 </DataTemplate>";

    gridHolesList.Columns[i].CellTemplate = XamlReader.Parse(cellTemplate) as DataTemplate;
    gridHolesList.Columns[i].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;
}