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

How to bind text property to TextBlock inside RowDetailTemplate

2 Answers 451 Views
GridView
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
Hello,

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" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="371" Width="747" xmlns:my="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView">  
    <Grid> 
        <Grid.RowDefinitions> 
            <RowDefinition></RowDefinition>  
            <RowDefinition Height="Auto" MinHeight="19"></RowDefinition> 
        </Grid.RowDefinitions> 
          
        <my:RadGridView Grid.Row="0" Name="radGridView1" RowDetailsVisibilityMode="VisibleWhenSelected">  
            <my:RadGridView.RowDetailsTemplate> 
                <DataTemplate> 
                    <StackPanel Orientation="Horizontal" Margin="10,10,10,10">  
                        <TextBlock Text="Detail from textBox1 is : " /> 
                        <TextBlock Text="{Binding ElementName=textBox1, Path=Text}" /> 
                    </StackPanel> 
                </DataTemplate> 
            </my:RadGridView.RowDetailsTemplate> 
        </my:RadGridView> 
          
        <StackPanel Grid.Row="1" Orientation="Horizontal">  
            <TextBox Width="100" Name="textBox1"></TextBox> 
            <TextBox Width="100" Text="{Binding ElementName=textBox1, Path=Text}" Name="textBox2"></TextBox>      
        </StackPanel> 
          
    </Grid> 
</Window> 
 
 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()  
        {  
            InitializeComponent();  
            DataToGrid1();  
        }  
 
        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);  
            }  
            myDataTable.AcceptChanges();  
 
            radGridView1.ItemsSource = myDataTable;    
 
        }  
    }  
}  
 

2 Answers, 1 is accepted

Sort by
0
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.

Regards,
Ross
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.
0
Tatu Nokelainen
Top achievements
Rank 1
answered on 25 Feb 2010, 02:07 PM
This works great, thank you so much for fast support!
Tags
GridView
Asked by
Tatu Nokelainen
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Tatu Nokelainen
Top achievements
Rank 1
Share this question
or