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

  • Thread starter Thread starter Sheikh Shuaib
  • Start date Start date
S

Sheikh Shuaib

Guest
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;
McDataGrid.Columns.Add(templateColumn);

Continue reading...
 
Back
Top