代码之家  ›  专栏  ›  技术社区  ›  Emad

WPF DataGrid绑定到datatable

  •  6
  • Emad  · 技术社区  · 16 年前

    基本上,我循环遍历我的datatable并为每一列创建DataGrid列,如下所示:

    private static void CreateDataGridColumns(DataGrid datagrid, Document doc)
    {
        if (doc == null) return; //return 
    
        datagrid.Columns.Clear();
        foreach (var item in doc.Keys)
        {
            var column = new DataGridTemplateColumn
            {
               Header = item,
               CellTemplateSelector = new CustomRowDataTemplateSelector(),
             };
    
            datagrid.Columns.Add(column);
        }
    }
    

    如您所见,我使用的是自定义数据模板选择器,因此可以根据单元格的内容以不同的方式呈现单元格。

    这是模板选择器

    public class CustomRowDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate
            SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;
    
            var presenter = container as ContentPresenter;
            var gridCell = presenter.Parent as DataGridCell;
    
            if (element != null && item != null && gridCell != null)
            {
                var row = item as DataRow;
    
                if (row != null)
                {
                    var cellObject = row[gridCell.Column.DisplayIndex];
    
                    //set template based on cell type
    
                    if (cellObject is DateTime)
                    {
                        return element.FindResource("dateCell") as DataTemplate;
                    }
    
                    return element.FindResource("stringCell") as DataTemplate;
                }
    
    
            }
    
            return null;
        }
    }
    

    <DataTemplate x:Key="stringCell">
        <StackPanel>
            <TextBlock Style="{StaticResource cellStyle}" 
                        Grid.Row="0" Grid.Column="0" 
                        Text="{Binding Converter={StaticResource cellConverter}}" />
        </StackPanel>
    </DataTemplate>
    

    <DataTemplate x:Key="stringCell">
        <StackPanel>
            <TextBlock Style="{StaticResource cellStyle}" 
                        Grid.Row="0" Grid.Column="0" 
                        Text="{Binding Path=Row[CellIndex], Converter={StaticResource cellConverter}}" />
        </StackPanel>
    </DataTemplate>
    

    但是我没有什么可以得到的。如何设置Path=Row[CellIndex]来执行类似的操作

    2 回复  |  直到 10 年前
        1
  •  0
  •   Sergey Krivospitskiy CFA    10 年前

    您可以尝试在代码中创建绑定。像这样的东西应该管用

    var bind = new Binding(gridCell.Column.Header.ToString())
    bind.Mode = BindingMode.TwoWay;
    bind.Source = row;
    BindingOperations.SetBinding(YourTextBlock, TextBlock.TextProperty, bind);
    
        2
  •  -1
  •   paparazzo    15 年前

    不确定你在功能上想要达到什么。你可以用代码来做。创建一个更高级别的类CellClass,它具有DisplayValue属性。实现日期和字符串。将源绑定到路径为DisplayValue的CellClass。您甚至可以创建List CellClass并绑定到CellClass[0],CellClass[1]。。。在我知道这是工作,因为我这样做,但我不知道它是否提供的功能,你正在寻找。

        public abstract class CellClass
        {
             public abstract String DispValue { get; }
        }
        public class CellClassDate : CellClass
        {
             public override String DispValue { get ...; }
             public DateTime DateValue { get .. set ... }
        }
        public class CellClassString : CellClass
        {
             public override String DispValue { get ...; }
             public DateTime StringValue { get .. set ... }
        }
    
    推荐文章