FlexGrid (WPF): Column Binding in Codebehind is NOT effective

Posted by: trancuong200495 on 6 May 2018, 9:15 pm EST

    • Post Options:
    • Link

    Posted 6 May 2018, 9:15 pm EST

    Dear C1 Support Team

    Because my requirements I need dynamic generate column in code behind, and moreover It has Binding Converter, ConverterParameter. So I do it as below but it NO call to Converter when generate column. I spent much time to debug and found solution but I can’t . Could you to see and Show me any wrong. I am so appreciate !

    private static void ColumnsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

    {

    C1FlexGrid grid = d as C1FlexGrid;

    if (grid == null || e.NewValue == null) return;

            IEnumerable<IHeaderCell> cols = e.NewValue as IEnumerable<IHeaderCell>;
            if (cols == null) return;
    
            int num = 0;
    
            foreach (IHeaderCell col in cols)
            {
                Column gridViewColumn = new Column();
                if (!string.IsNullOrEmpty(col.CellConverterKey) && grid.Resources.Contains(col.CellConverterKey))
                {
                    var binding = new Binding();
                    binding.Converter = grid.Resources[col.CellConverterKey] as IValueConverter;
                    binding.ConverterParameter = col;
                    
                    gridViewColumn.Binding = binding;
                }
    
                if (col.CellTemplateKeySelector != null)
                {
                    string cellTemplateResourceKey = col.CellTemplateKeySelector(null);
                    if (grid.Resources.Contains(cellTemplateResourceKey))
                    {
                        gridViewColumn.CellTemplate = grid.Resources[cellTemplateResourceKey] as DataTemplate;
                    }
                }
    
                grid.Columns.Add(gridViewColumn);
    
                ++num;
            }
        }
    
  • Posted 7 May 2018, 4:06 pm EST

    If I generate column in code behind and then set both Binding and CellTemplate, Binding is not effective, if I only add binding it operate ok ? why?

  • Posted 7 May 2018, 6:13 pm EST

    Hello,

    If you wish to dynamically set CellTemplate, Binding Converter, ConverterParameter in code-behind, then I would suggest you to use CellFactory. CellFactory consists of various methods, any of which can be overridden. I have attached a demo application ‘DynamicImages’ setting Converter etc for choosen columns.

    In case you wish to do that in XAML, then for that also I have attached a sample ‘ConditionalFormatting’ setting Binding Converter, ConverterParameter etc in xaml.

    Also, for learning more about C1FlexGrid in WPF, I suggest you to refer various product samples installed at ‘\Documents\ComponentOne Samples\WPF\C1.WPF.FlexGrid\CS

    Thanks,

    Ruchir Agarwal

    ConditionalFormatting.zip

    DynamicImages.zip

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels