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
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.
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
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.
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
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.
