access object in codebehind

3 posts, 0 answers
  1. Bart
    Bart avatar
    20 posts
    Member since:
    May 2014

    Posted 17 Feb 2017 Link to this post

    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. Martin Ivanov
    Martin Ivanov avatar
    2403 posts

    Posted 21 Feb 2017 Link to this post

    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.
  3. Bart
    Bart avatar
    20 posts
    Member since:
    May 2014

    Posted 21 Feb 2017 Link to this post

    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.
Back to Top