How to created datagridtemplatecolumn in datagrid dynamically in wpf with textbox from code behind RRS feed

  • Question

  • i want to add datagridtemplatecolumn in datagrid dynamically from code behind. Following is what i want to achive from code behind:

    <DataGridTemplateColumn Header="Rate" MinWidth="80"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox x:Name="AP_Rate" FontSize="15" Height="40" Text="{Binding ApRate,Mode=OneWay}" TextWrapping="Wrap" VerticalAlignment="Top" MinWidth="60"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn>

    Also i want to bind textbox to "ApRate" and binding mode to "One Way"

    I tried the following:

    DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
                    templateColumn.Header = "Rate";
                    var factory = new FrameworkElementFactory(typeof(TextBox));
                    factory.Name = "ApRate";
                    factory.SetBinding(TextBox.TextProperty, new Binding("ApRate"));
                    DataTemplate cellEditingTemplate = new DataTemplate();
                    cellEditingTemplate.VisualTree = factory;
                    templateColumn.CellEditingTemplate = cellEditingTemplate;

    Thursday, April 2, 2020 12:56 PM

All replies

  • Hi Sheikh Shuaib,
    Based on your description, it is related to WPF. So it is recommended to ask the questions in this forum and you can get more professional answer.
    Thank you for your understanding.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 3, 2020 5:35 AM