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

Row Details

6 Answers 85 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ron klod
Top achievements
Rank 1
ron klod asked on 16 Mar 2010, 10:52 AM
Hi,

I am using the raw details feature.
in the row details I have for example a textblock
my question is how can I get a reference to that textblock from the code behind?

Ron

6 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 16 Mar 2010, 11:13 AM
Hi ron klod,

There are several ways to get elements that are contains in RowDetails depending on your scenario. For example, if you would like to get an element whenever a row is clicked (details is shown) you can use the RowDetailsVisibilityChanged event:

// required namespace Telerik.Windows.Controls
private void playersGrid_RowDetailsVisibilityChanged(object sender, GridViewRowDetailsEventArgs e)
{
    // find needed element
    var textBlock = e.DetailsElement.ChildrenOfType<TextBlock>();
}

If you would like to get an element in the selected row you could try something like this:

// gets the TextBlock in the currently selected item
private TextBlock GetTextBlockFromRowDetails()
{
    var selectedRow = (GridViewRow)this.myGrid.ItemContainerGenerator.ContainerFromItem(this.myGrid.SelectedItem);
    // container for RowDetails elements
    var detailsPresenter = selectedRow.ChildrenOfType<DetailsPresenter>().FirstOrDefault();
    // find needed element
    var textBlock = detailsPresenter.ChildrenOfType<TextBlock>().FirstOrDefault();
  
    return textBlock;
}

Best wishes,
Milan
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
ron klod
Top achievements
Rank 1
answered on 16 Mar 2010, 09:06 PM
Hi,

1. this is not good for me, I want to get the reference in the Loaded event, lets say I want to do some operaqtion on the texbox in th loaded time, for example:
lets say I want to set the text property of the textbox in the code behind(I know that I can use the {Binding } in the xaml, this is just for example)

how should I do that?

2. another problem which I have:
    the grid I am using as a column that has combox inside of it. in each row details view there is a button, what I want to do is:
    only if th euser selected a specific item in the combox in the current row then the button wil become enable
    my problem is that I don't know how to get a reference to the button in the combobox change event.
    I tried this:
   

ComboBox

 

combo = (sender as ComboBox);

 

 

var row = combo.ParentOfType<GridViewRow>();

 

row.GridViewDataControl.RowDetailsTemplate // but this does not return anything
how should I so this?



Ron
0
Milan
Telerik team
answered on 18 Mar 2010, 04:37 PM
Hello ron klod,

Well you could subscribe to the Loaded event of your details template and initialize any properties or controls there.

In regard to the second question, the task can be accomplished really easy if the combobox actually reflect a real property on the data objects. For example, imagine that the grid is bound to Person objects which have a property Country and this property is represented by a ComboBox editor which allows you to change the country of each person. In this situation you can easily bind the IsEnabled property of the button that is placed in row details. 

I am sending you a sample project that demonstrates this approach. Notice that the button is only enabled when France is set as country.

Kind regards,
Milan
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
ron klod
Top achievements
Rank 1
answered on 18 Mar 2010, 11:45 PM
regarding the the answer for the first part, The problem is that since it is inside the row details template, I can change it it the loaded event, it does not know it then
0
ron klod
Top achievements
Rank 1
answered on 26 Mar 2010, 02:39 PM
why can't I use anythoing inside the row details section in the loaded evet?
for exmple I ahve a textblocl by the name of txt1.
I want to be able to do this in the code behind
txt1.Text="";

but since the txt1 is inside a rowdetailstemplate i can get a reference to it in the loaded event of my usercontrol

Ron
0
Vlad
Telerik team
answered on 26 Mar 2010, 02:50 PM
Hi,

What if you have at least two rows? You will have at least two TextBlocks. That is why elements inside DataTemplates cannot be accessed in your page context and in this case you can use our powerful extension methods ChildrenOfType<>() and ParentOfType<>(). For example:

var row = ((UserControl)sender).ParentOfType<GridViewRow>();
var textBlock = row.ChildrenOfType<TextBlock>().Where(tb => tb.Name == "YourName").FirstOrDefault();

if (textBlock != null)
{
     textBlock.Text = "YourText";
}

Kind regards,
Vlad
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.
Tags
GridView
Asked by
ron klod
Top achievements
Rank 1
Answers by
Milan
Telerik team
ron klod
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or