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

How to reference object in DataTemplate?

2 Answers 144 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phil Zheng
Top achievements
Rank 1
Phil Zheng asked on 07 Jul 2010, 05:59 PM
Please see the attached codes. I need to access "_options" from code behind to set its ItemsSource. How do I achieve that?

Thanks,
Phil

<Controls:RadWindow x:Class="Sketch.SilverUI.BadItemDlg" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" mc:Ignorable="d" 
    d:DesignHeight="223" d:DesignWidth="400" Height="400" Width="400" Header="Bad Items" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" CanClose="False" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">  
 
    <Controls:RadWindow.Resources> 
        <DataTemplate x:Key="ComboResource">  
            <Grid> 
                <StackPanel Orientation="Horizontal" Margin="0, 5, 0, 0">  
                    <telerik:RadComboBox ItemsSource="{Binding Path=AvailableItems}" Height="20" Width="100" DisplayMemberPath="DisplayName" SelectedIndex="{Binding Path=SelectedInx}" HorizontalAlignment="Center"></telerik:RadComboBox> 
                </StackPanel> 
            </Grid> 
        </DataTemplate> 
    </Controls:RadWindow.Resources> 
      
    <Grid x:Name="LayoutRoot" Background="White" Height="369">  
  
  
        <telerik:RadGridView Height="254" RowIndicatorVisibility="Collapsed" HorizontalAlignment="Left" Margin="25,63,0,0" Name="_grid" VerticalAlignment="Top" Width="325" AutoGenerateColumns="False">  
            <telerik:RadGridView.Columns> 
              
                
                <telerik:GridViewDataColumn Header="Available Items" Width="133">                      
                    <telerik:GridViewDataColumn.CellTemplate> 
                        <DataTemplate> 
                            <telerik:ItemsControl ItemTemplate="{StaticResource ComboResource}" HorizontalAlignment="Left" Name="_options"/>  
                        </DataTemplate> 
                    </telerik:GridViewDataColumn.CellTemplate> 
                </telerik:GridViewDataColumn> 
            </telerik:RadGridView.Columns>              
        </telerik:RadGridView> 
       
 
    </Grid> 
</Controls:RadWindow> 

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 07 Jul 2010, 06:26 PM
Hello Phil Zheng,

You may expose the items source for the _options ItemsControl  as a static resource on the user control.

Then you may bind the ItemsSource  with syntax like :
ItemsSource = "{Binding ... Source ={StaticResource ..}".

This in case you have the same items for each row.

In case you need to set the ItemsSource individually per row,
You will need to subscribe to the RowLoaded event of RadGridView. Within the event handler you get the row (e.Row)  and then -
You may find the instance of the Items control with something like :

var myItemsControl e.Row.ChildrenOfType<ItemsControl>().FirstOrDefault();

Kind regards,
Pavel Pavlov
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
0
Phil Zheng
Top achievements
Rank 1
answered on 07 Jul 2010, 11:40 PM
Thanks, that works.
Tags
GridView
Asked by
Phil Zheng
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Phil Zheng
Top achievements
Rank 1
Share this question
or