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

access object in codebehind

2 Answers 953 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bart
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.

 

                    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 Answers, 1 is accepted

Sort by
0
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");

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.
0
Bart
Top achievements
Rank 1
answered on 21 Feb 2017, 10:56 AM

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.
Tags
General Discussions
Asked by
Bart
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Bart
Top achievements
Rank 1
Share this question
or