access object in codebehind

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

    Posted 17 Feb 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.

     

                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                        xmlns:telerikScheduleView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.ScheduleView"
                        xmlns:telerikColorPickers="clr-namespace:Telerik.Windows.Controls.RichTextBoxUI.ColorPickers;assembly=Telerik.Windows.Controls.RichTextBoxUI"
                        xmlns:local="clr-namespace:Proj"
                        xmlns:p="clr-namespace:Proj.Resources"
                        x:Class="Proj.ProductieOrderViewRecources">
     
     
        <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">
            <Grid>
                <telerik:RadBusyIndicator x:Name="BusyIndicator" Grid.Row="2" Visibility="{Binding ShowPlanning}" >
                    <telerik:RadGridView x:Name="gridViewAdminData"
                                         ItemsSource="{Binding DataRecords}"
                                         RowEditEnded="EditRecord"
                                         DataLoading="LoadData"
                                         SelectionChanged="GridSelectionChanged"
                                         FrozenColumnCount ="5"
                                         AutoGenerateColumns="False"
                                         Deleting="RadGridView_deleting"
                                         SelectedItem="{Binding SelectedPlanning}">
                        <telerik:RadGridView.Columns>
                            <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()
        {
            InitializeComponent();
             
                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
    Admin
    Martin Ivanov avatar
    1352 posts

    Posted 21 Feb 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");

    Regards,
    Martin
    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
    18 posts
    Member since:
    May 2014

    Posted 21 Feb Link to this post

    hi,
    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