tags:

views:

207

answers:

1

I am implementing the WPF DataGrid (very new to WPF). I followed tutorials that showed how to bind the comboboxcolumn using staticresources. However, the databinding for a few columns in my datagrid will not be known until runtime.

Because of this, I can't bind them with the staticresource. Is there any other way to databind the comboboxcolumns in a datagrid? In ASP.NET, I know we had the rowdatabound code where we could do this and dynamically create the contents of the columns. But, in WPF, it looks like everything is done through resources.

How can you databind using dynamic resources in the datagrid?

Thanks!

A: 

You can set up bindings dynamically. Something like this (this code creates grid view columns and assigns dynamic bindings)

       private void AddColumn(GridView view, Field fld)
        {
            GridViewColumn col = new GridViewColumn();
            col.Header = fld.Label;
            Binding bnd = new Binding();
            switch (fld.FieldType)
            {
                case FieldType.DateTime:
                bnd.Converter = new DateTimeToDateStringConverter();
                break;
// or some other converters
            }
            bnd.Path = new PropertyPath(string.Format("Fields[{0}]",
    _table._fields.IndexOf(fld)));  // the string matches what you would use in XAML
            col.DisplayMemberBinding = bnd;
            view.Columns.Add(col);
        }
Sergey Aldoukhov