使用Binded ColumHeaders构建动态DataGrid

时间:2017-05-25 14:14:30

标签: c# wpf datagrid

我正在动态构建DataGrid,我会根据需要显示列,但我的列标题没有显示任何内容。

我尝试为每列设置HeaderTemplate,但我的最终DataGrid显示空标题。

我的DataGrid生成器:

private void ImportarView_ColumnsToMapChanged(object sender, ColumnsToMapChangedEventArgs e)
{
    ImportColumns = e.Columns;
    Importar source = ((Importar)GrdMain.DataContext);
    DGrdDatosImportar.Columns.Clear();

    foreach (ColumnParms columnparms in ImportColumns)
    {
        string ColName = $"Col{columnparms.ColumnName}";
        ImportColumn Col = new ImportColumn()
        {
            Binding = new Binding($"{columnparms.ColumnName}.DisplayValue"),
            Visibility = Visibility.Visible,
            ColParms = columnparms
        };
        Col.SetNameScope(ColName,this);

        var MyTb = new FrameworkElementFactory(typeof(TextBlock));
        //Binding txtBind = new Binding($"ElementName={ColName},Path=ColParms.ColumnName");
        Binding txtBind = new Binding("TextProperty")
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, Col.GetType(), 1),
            Path = new PropertyPath(Col.ColParms.ColumnName)                   
        };
        MyTb.SetBinding(TextBlock.TextProperty, txtBind);
        MyTb.SetValue(TextBox.VisibilityProperty, Visibility.Visible);
        DataTemplate MyHeaderTemplate = new DataTemplate() { VisualTree = MyTb };
        Col.HeaderTemplate = MyHeaderTemplate;

        Col.Visibility = Visibility.Visible;
        DGrdDatosImportar.Columns.Add(Col);
    }
}

我的专栏:

public class ImportColumn : DataGridTextColumn 
{
    #region Column Parms
    public ColumnParms ColParms
    {
        get { return (ColumnParms)GetValue(ColParmsProperty); }
        set { SetValue(ColParmsProperty, value); }
    }
    public static readonly DependencyProperty ColParmsProperty =
        DependencyProperty.Register(
            "ColParms", typeof(ColumnParms), typeof(ImportColumn));
    #endregion

    private NameScope ColumnScope = new NameScope();

    public void SetNameScope(string name,object scope)
    {
        NameScope.SetNameScope(this,ColumnScope);
        ColumnScope.RegisterName(name, scope);
    }
}

这是我的最终DataGrid数据,但没有标题:

以编程方式设置标头的方法是什么?

注意:我的Column类已经引用了自己的绑定源。

1 个答案:

答案 0 :(得分:0)

最后通过组合我的标题

的两个属性来解决我的问题
Header =  columnparms

Header Act作为我的ColumnHeader的DataConext然后,用我的文本框设置HeaderTemplate

    private void ImportarView_ColumnsToMapChanged(object sender, ColumnsToMapChangedEventArgs e)
    {
        ImportColumns = e.Columns;
        Importar source = ((Importar)GrdMain.DataContext);
        DGrdDatosImportar.Columns.Clear();

        foreach (ColumnParms columnparms in ImportColumns)
        {
            #region Crear la columna
            ImportColumn Col = new ImportColumn()
            {
                Header = columnparms,
                Binding = new Binding($"{columnparms.ColumnName}.DisplayValue"),
                Visibility = Visibility.Visible,
                ColParms = columnparms
            };
            #endregion

            #region Text block Nombre de la columna
            Binding txtBind = new Binding("TextProperty") { Path = new PropertyPath("DisplayString", null) };
            var MyTb = new FrameworkElementFactory(typeof(TextBlock));
            MyTb.SetBinding(TextBlock.TextProperty, txtBind);
            #endregion

            Col.HeaderTemplate = new DataTemplate() { VisualTree = MyTb };

            DGrdDatosImportar.Columns.Add(Col);
        }
    }