This is a migrated thread and some comments may be shown as answers.

How to bind text property to TextBlock inside RowDetailTemplate

2 Answers 450 Views
This is a migrated thread and some comments may be shown as answers.
Tatu Nokelainen
Top achievements
Rank 1
Tatu Nokelainen asked on 25 Feb 2010, 10:54 AM

To simplify my question, I have project with one RadGridView and 2 text boxes. textBox2 is binded to textBox1 and TextBlock inside RowDetailsTemplate is also binded to that same textBox1 exactly same way. Binding to inside RowDetailsTemplate does not work, binding from textBox1 to textBox2 works ok.
Can you please point me what I do wrong?

XAML is:
<Window x:Class="WpfApplication11.Window1" 
    Title="Window1" Height="371" Width="747" xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView">  
            <RowDefinition Height="Auto" MinHeight="19"></RowDefinition> 
        <my:RadGridView Grid.Row="0" Name="radGridView1" RowDetailsVisibilityMode="VisibleWhenSelected">  
                    <StackPanel Orientation="Horizontal" Margin="10,10,10,10">  
                        <TextBlock Text="Detail from textBox1 is : " /> 
                        <TextBlock Text="{Binding ElementName=textBox1, Path=Text}" /> 
        <StackPanel Grid.Row="1" Orientation="Horizontal">  
            <TextBox Width="100" Name="textBox1"></TextBox> 
            <TextBox Width="100" Text="{Binding ElementName=textBox1, Path=Text}" Name="textBox2"></TextBox>      
 and the code behind is:
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Data;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Navigation;  
using System.Windows.Shapes;  
using System.Data;  
namespace WpfApplication11  
    /// <summary> 
    /// Interaction logic for Window1.xaml  
    /// </summary> 
    public partial class Window1 : Window  
        public Window1()  
        private void DataToGrid1()  
            DataTable myDataTable = new DataTable();  
            myDataTable.Columns.Add("Id", typeof(int));  
            myDataTable.Columns.Add("Model", typeof(string));  
            myDataTable.Columns.Add("Color", typeof(string));  
            myDataTable.Columns.Add("Prize", typeof(double));  
            for (int i = 0; i < 6; i++)  
                myDataTable.Rows.Add(i, "BMW", "Blue", 5700);  
            radGridView1.ItemsSource = myDataTable;    

2 Answers, 1 is accepted

Sort by
Rossen Hristov
Telerik team
answered on 25 Feb 2010, 01:49 PM
Hi Tatu Nokelainen,

You cannot do this. The TextBlock inside the RowDetailsTemplate cannot "see" the TextBox by ElementName, since it is inside a DataTemplate and is not a "neighbour" if the TextBox. We have to wait until this TextBlock is actually "born" in order to bind it. And it is "born" when the RowDetailsTemplate is loaded.

Here is what you need to do.

1. Give the target TextBlock a name, for example rowDetailsTextBlock and remove its Binding.
2. Attach to the LoadingRowDetailsEvent of RadGridView.
3. Bind the target to the source in in the event handler:

private void radGridView1_LoadingRowDetails(object sender, Telerik.Windows.Controls.GridView.GridViewRowDetailsEventArgs e)
    var target = e.DetailsElement.FindName("rowDetailsTextBlock") as TextBlock;
    if (target != null)
        Binding textBinding = new Binding("Text") { Source=this.textBox1, };
        target.SetBinding(TextBlock.TextProperty, textBinding);

This will do the trick. I have attached a sample project. I hope this helps.

the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tatu Nokelainen
Top achievements
Rank 1
answered on 25 Feb 2010, 02:07 PM
This works great, thank you so much for fast support!
Asked by
Tatu Nokelainen
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Tatu Nokelainen
Top achievements
Rank 1
Share this question