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

access object in codebehind

2 Answers 731 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Bart asked on 17 Feb 2017, 02:22 PM

Hi Guys,

I'm struggling with an issue and I can't figure why this goes wrong.

I cannot access my object in the resourcedictionary in my code behind.  What I have is the following
I have a resourcedictionary like this.


    <DataTemplate x:Key="ProductieOrderViewToolBarTemplate">
        <telerik:RadRichTextBoxRibbonUI x:Name="richTextBoxRibbonUI"


this whitin this resourcedictionary
I have a radgridview like this.

<DataTemplate x:Key="ProductieOrderViewMainSectorTemplate">
    <Grid Margin="10">
            <telerik:RadBusyIndicator x:Name="BusyIndicator" Grid.Row="2" Visibility="{Binding ShowPlanning}" >
                <telerik:RadGridView x:Name="gridViewAdminData"
                                     ItemsSource="{Binding DataRecords}"
                                     FrozenColumnCount ="5"
                                     SelectedItem="{Binding SelectedPlanning}">
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Artikel}" IsReadOnly="True" />
                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Aantal}" />

now I'm trying the access the gridViewAdminData by name in the code behind.

but it won't work at all.
I have this in my code

public partial class ProductieOrderViewRecources : ResourceDictionary
    public ProductieOrderViewRecources()
            System.Linq.Expressions.Expression<Func<Proj.Models.productieorder, double>> expression = order => (order.AantalTeProduceren + 10);
            //RadGridView gridViewAdminData = (RadGridView)this["gridViewAdminData"];
            GridViewExpressionColumn column = gridViewAdminData.Columns["TotalValue"] as GridViewExpressionColumn;
            column.Expression = expression;

but gridViewAdminData won't get recognized. Also enabling the line above gives back a null value.

Does anyone have any idea about how to access this object in the code behind?

thank you

2 Answers, 1 is accepted

Sort by
Martin Ivanov
Telerik team
answered on 21 Feb 2017, 09:05 AM
Hello Bart,

You can't access a UIElement that is defined in a DataTemplate directly in code-behind using its Name. To modify a UIElement in a template you can use data binding. Or you get the element after it loads using code. There is an example in stackoverflow. Alternatively, you can use the ChildrenOfTypeExtensions.ChildrenOfType<T>() method to find the gridview instance after it is loaded. For example:
ChildrenOfTypeExtensions.ChildrenOfType<RadGridView>(parentElementThatHostsTheDataTemplate).FirstOrDefault(x => x.Name == "gridViewAdminData");

Telerik by Progress
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Top achievements
Rank 1
answered on 21 Feb 2017, 10:56 AM

thank you for the reply

I've tried a lot, but I already went with the onload event

private void LoadData(object sender, GridViewDataLoadingEventArgs e)
           rgv = (RadGridView)sender;
           Expression<Func<proj.Models.planning, double>> expression = planning => ((planning.Aantal ?? 0) + planning.TotalStock);
           GridViewExpressionColumn column = rgv.Columns["TotalValue"] as GridViewExpressionColumn;
           column.Expression = expression;

which works for me.
General Discussions
Asked by
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Top achievements
Rank 1
Share this question