Adding DataGrid RowDetailsTemplate or TemplateColumn in Code Bhind

27 01 2010

Create a DataTemplate structure in resources.

Then we can  load the DataTemplete and set it to DataGrid rowDetailsTemplate /TemplateColumn

We can use a StackPanel/Grid inside the DataTemplate and set load event on StackPanel to add new children at runtime.

Example below:

XAML:

<UserControl.Resources>
<DataTemplate x:Key=“rowDT”>
<StackPanel Loaded=“StackPanel_Loaded” Orientation=“Horizontal” Background=“White”>

</StackPanel>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name=“LayoutRoot” Background=“Wheat”>
<data:DataGrid x:Name=“myDataGrid” AutoGenerateColumns=“False”>
</data:DataGrid>
</Grid>

Code Behind For RowDetailsTemplate:

        public Page()
        {
            InitializeComponent();

            var rowDetailsTemplate = (DataTemplate)this.Resources["rowDT"];
            myDataGrid.RowDetailsTemplate = rowDetailsTemplate;
            myDataGrid.ItemsSource = GridData.GetGridData();
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var templateChildPanel = sender as StackPanel;
            var detailTextBlock = new TextBlock
            {
                Width = 350,
                TextWrapping = TextWrapping.Wrap,
                Text = "I am Amyo Kabir"
            };

            //If you need binding
            //var dataBinding = new Binding("DetailsText");
            //dataBinding.Mode = BindingMode.TwoWay;
            //detailTextBlock.SetBinding(TextBlock.TextProperty, dataBinding);
            templateChildPanel.Children.Add(detailTextBlock);
        }

Code Behind For TemplateColumn:

        public Page()
        {
            InitializeComponent();

            var cellTemplate = (DataTemplate)this.Resources["rowDT"];

            var dataGridTemplateColumn = new DataGridTemplateColumn();
            dataGridTemplateColumn.Header = "Run Time Column";
            dataGridTemplateColumn.CellTemplate = cellTemplate;

            myDataGrid.Columns.Add(dataGridTemplateColumn);
        }

        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            var templateChildPanel = sender as StackPanel;
            var detailTextBlock = new TextBlock
            {
                Width = 350,
                TextWrapping = TextWrapping.Wrap,
                Text = "I am Amyo Kabir"
            };

            //If you need binding
            //var dataBinding = new Binding("DetailsText");
            //dataBinding.Mode = BindingMode.TwoWay;
            //detailTextBlock.SetBinding(TextBlock.TextProperty, dataBinding);
            templateChildPanel.Children.Add(detailTextBlock);
        }
Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: