How do I reference RowDetails in code-behind?

4 posts, 0 answers
  1. Rob Ainscough
    Rob Ainscough avatar
    262 posts
    Member since:
    Jan 2010

    Posted 24 Jul 2012 Link to this post

    I have a RadGridView that has RowDetailsTemplate assign to DataTemplate defined in my App.xaml.

    <telerik:RadGridView x:Name="TransactionGridView"
                                   Margin="0" FontSize="10.667"                                       
                                   AreRowDetailsFrozen="True"
                                   AutoGenerateColumns="False"
                                   CanUserFreezeColumns="False"
                                   CanUserReorderColumns="False"
                                   CanUserResizeColumns="False"
                                   CanUserSortColumns="False"
                                   RowIndicatorVisibility="Collapsed"
                                   IsReadOnly="True"
                                   DataContext="{Binding Source={StaticResource TransactionsData}}"
                                   ItemsSource="{Binding Collection}"
                                   RowDetailsTemplate="{StaticResource TransactionDetailDataTemplate}"
                                   ShowGroupPanel="False">
               <telerik:RadGridView.Columns>
                   <telerik:GridViewToggleRowDetailsColumn />
                   <telerik:GridViewDataColumn Header="TxID" IsVisible="False" DataMemberBinding="{Binding TxID}" />
                   <telerik:GridViewDataColumn Header="Date" MaxWidth="65" MinWidth="65" Width="65" DataMemberBinding="{Binding TxDate}" />
                   <telerik:GridViewDataColumn Header="Description" MaxWidth="113" MinWidth="113" Width="113" DataMemberBinding="{Binding TxDescription}" />
                   <telerik:GridViewDataColumn Header="Amt." MaxWidth="60" MinWidth="60" Width="60" TextAlignment="Right" DataMemberBinding="{Binding TxAmount}" />
                   <telerik:GridViewDataColumn Header="Other" MaxWidth="55" MinWidth="55" Width="55" TextAlignment="Right" DataMemberBinding="{Binding Other}" />
                   <telerik:GridViewDataColumn Header="Bal." MaxWidth="60" MinWidth="60" Width="60" TextAlignment="Right" DataMemberBinding="{Binding Balance}" />
               </telerik:RadGridView.Columns>
           </telerik:RadGridView>

    Here is the DataTemplate in my App.xaml:

    <DataTemplate x:Key="TransactionDetailDataTemplate">
        <Grid MinHeight="28" Style="{StaticResource RowDetailBackground}">
     
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="410"/>
                <ColumnDefinition Width="85"/>
                <ColumnDefinition Width="60"/>
                <ColumnDefinition Width="230"/>
            </Grid.ColumnDefinitions>
     
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
             
            <ListBox x:Name="TransactionDetailListBox" Grid.Column="0" Grid.ColumnSpan="4" Margin="100,0,0,0" Background="{x:Null}" DataContext="{Binding Source={StaticResource TransactionDetailData}}" BorderBrush="{x:Null}" FontSize="10.667">
                <ListBoxItem Content="{Binding Collection[0].Description1}" />
            </ListBox>
             
            <CheckBox x:Name="aCheckBox" Content="Something" Grid.Column="1" Grid.ColumnSpan="1" Style="{StaticResource CheckBox}" Visibility="Collapsed" />
            <sdk:Label x:Name="aReasonLabel" Grid.Column="2" Content="Reason:" Style="{StaticResource LabelRight}" Visibility="Collapsed" />
            <TextBox x:Name="aTextBox" Grid.Column="3" Grid.ColumnSpan="1"  Style="{StaticResource TextBoxLeftFill}" Visibility="Collapsed" />
             
        </Grid>
    </DataTemplate>

    How can I reference/use aCheckBox or aReasonLabel or aTextBox in my code-behind?

    Thanks, Rob.
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 25 Jul 2012 Link to this post

    Hello,

     Please check this help article for a reference.

    Regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Rob Ainscough
    Rob Ainscough avatar
    262 posts
    Member since:
    Jan 2010

    Posted 25 Jul 2012 Link to this post

    Didie,

    Thanks for the response, that worked for control reference.  However, since my DataTemplate is defined in my App.Xaml (it's used "global" by my application), if I want to have an event (say click) on the aCheckBox control, I've added:
    <CheckBox x:Name="aCheckBox" Content="Something" Grid.Column="1" Grid.ColumnSpan="1" Click="SomethingCheckBox_Click"  Style="{StaticResource aCheckBoxStyle}" Visibility="Collapsed" />

    So now I added the method to my App.xaml.vb:
    Private Sub SomethingCheckBox_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
     
            ' Change the visible state of a label - how to reference label control?        
     
    End Sub

    The event will trigger on the aCheckBox click (in my App.xaml.vb) but at this point (in my SomethingCheckBox_Click) method there is no e.DetailsElement available (can't use code below):
    e.DetailsElement.FindName("aReasonLabel")
     
    So the question is now shifted to: How can I reference a control in my DataTemplate that is in App.Xaml from within a control event triggered by another control in that same DataTemplate using code-behind in my App.xaml.vb (see the comment in my 2nd code block above, that's what I want to accomplish)? 

    Now I know you probably want to say use MVVM, but lets avoid that debate and see how I can accomplish this without using MVVM.

    Thanks, Rob.




  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 25 Jul 2012 Link to this post

    Hello,

    The e.DetailsElement is available in the GridViewRowDetailsEventArgs and you could use it if you subscribe for the LoadingRowDetails event of the RadGridView.

    In your case you could find the parent StackPanel/DataTemplate of the clicked CheckBox using the ParentOfType<T> extension function. Once you have the parent element, you can find the control you are interested in using the ChildrenOfType<T> extension method.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top