UserControl in RadGridView Can't Reference in Code-Behind or ViewModel

3 posts, 0 answers
  1. Stephen
    Stephen avatar
    21 posts
    Member since:
    Dec 2013

    Posted 25 Jun 2015 Link to this post

    I have a user control.  My screen has a RadGridView, with a RowDetailsTemplate.  Inside the Data Template of the RowDetailsTemplate there is a RadTabControl.  My user control is on one of the tabs.  When it is like this, I cannot reference the x:Name of that user control.  If I move the whole RadTabControl outside of the RadGridView, I can reference the user control.  It also works fine if I move the user control out of the RadGridView itself.  So, it looks like the problem is that my code cannot find the user control, when it is inside a RadGridView RowDetailsTemplate.

    Here is my xaml code and this is the line with my user control in it:

    This is in the top declarations: xmlns:controls1="clr-namespace:MJS.Client.Controls"

    This is the user control line that is down in the RadTabItem code:  <controls1:AlertPerson x:Name="controlAlertPerson" />

    Here is the RadGridView:

    <StackPanel Grid.Column="0"
                Orientation="Horizontal">
        <telerik:RadGridView x:Name="Test1"
                             Width="1130"
                             MinHeight="25"
                             MaxWidth="1135"
                             MaxHeight="800"
                             Margin="4,4,4,4"
                             HorizontalAlignment="Right"
                             ItemsSource="{Binding SearchPersonCollection,
                                                   Mode=OneWay}"
                             RowActivated="rgvSearchPersonResults_RowActivated"
                             RowDetailsVisibilityChanged="rgvSearchPersonResults_RowDetailsVisibilityChanged"
                             RowDetailsVisibilityMode="Collapsed"
                             SelectedItem="{Binding Path=DataContext.SearchPersonSelected,
                                                    RelativeSource={RelativeSource FindAncestor,
                                                                                   AncestorType=telerik:RadBusyIndicator},
                                                    Mode=TwoWay}"
                             ShowGroupPanel="True"
                             Style="{StaticResource RadGridViewStandard}">
            <telerik:RadGridView.SortDescriptors>
                <telerik:SortDescriptor Member="FormattedName"
                                        SortDirection="Ascending" />
            </telerik:RadGridView.SortDescriptors>
            <telerik:RadGridView.Columns>
                <telerik:GridViewToggleRowDetailsColumn ExpandMode="Single"
                                                        ToggleButtonStyle="{StaticResource GridViewToggleButtonStyle}" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Source}"
                                            Header="Source"
                                            IsReadOnly="True" />
                <telerik:GridViewImageColumn DataMemberBinding="{Binding PersonPhotograph,
                                                                         Converter={StaticResource imageConverter}}"
                                             Header="Photo"
                                             ImageHeight="40"
                                             ImageStretch="UniformToFill"
                                             ImageWidth="40"
                                             IsReadOnly="True" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding FormattedNameWithAlias}"
                                            Header="Name"
                                            IsReadOnly="True" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding FormattedAddress}"
                                            Header="Address"
                                            IsReadOnly="True" />
                <telerik:GridViewDataColumn DataFormatString="{} {0:MM/dd/yyyy}"
                                            DataMemberBinding="{Binding ClientDateofBirth}"
                                            Header="Date Of Birth"
                                            IsReadOnly="True"
                                            UniqueName="DateOfBirth" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding ClientAge}"
                                            Header="Age"
                                            IsReadOnly="True" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding ClientSexCode}"
                                            Header="Sex"
                                            IsReadOnly="True" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding ClientRaceDescription}"
                                            Header="Race"
                                            IsReadOnly="True" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding FormattedIdentification}"
                                            Header="Identification"
                                            IsReadOnly="True" />
                <telerik:GridViewDataColumn DataMemberBinding="{Binding FormattedTelephoneDisplay}"
                                            Header="Phone"
                                            IsReadOnly="True" />
            </telerik:RadGridView.Columns>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SearchPersonSelectionChangedCommand}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <telerik:RadGridView.RowDetailsTemplate>
                <DataTemplate>
                    <telerik:RadTabControl x:Name="Test2"
                                           Margin="5,5,5,5"
                                           HorizontalAlignment="Left"
                                           VerticalAlignment="Center">
                        <telerik:RadTabItem x:Name="Test21"
                                            Header="Alerts">
                            <StackPanel Margin="5,5,5,5">
                                <controls1:AlertPerson x:Name="controlAlertPerson" />
                            </StackPanel>
                        </telerik:RadTabItem>
                        <telerik:RadTabItem x:Name="Test22"
                                            Header="Addresses">
                            <StackPanel Margin="5,5,5,5">
                                <telerik:RadGridView MaxWidth="1130"
                                                     BorderBrush="{x:Null}"
                                                     BorderThickness="0"
                                                     CanUserSelect="True"
                                                     Foreground="Black"
                                                     ItemsSource="{Binding AddressDetail,
                                                                           Mode=OneWay}"
                                                     Style="{StaticResource RadGridViewStandard}">
                                    <telerik:RadGridView.Resources>
                                        <Style x:Key="GridViewToggleButtonColumnStyle"
                                               TargetType="telerik:GridViewCell">
                                            <Setter Property="Padding" Value="0,0,0,0" />
                                        </Style>
                                    </telerik:RadGridView.Resources>
                                    <telerik:RadGridView.Columns>
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Address1}"
                                                                    Header="Street Addresss" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Address2}"
                                                                    Header="Apartment/Suite" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding CityCityName}"
                                                                    Header="City" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding ForeignCity}"
                                                                    Header="Foreign City" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding StateCode}"
                                                                    Header="State" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding ForeignState}"
                                                                    Header="Foreign State" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding PostalCode}"
                                                                    Header="Zip Code" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding CountryCountryName}"
                                                                    Header="Country" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding IsCurrent}"
                                                                    Header="Current" />
                                    </telerik:RadGridView.Columns>
                                </telerik:RadGridView>
                            </StackPanel>
                        </telerik:RadTabItem>
                        <telerik:RadTabItem x:Name="Test23"
                                            Header="Identifications">
                            <StackPanel Margin="5,5,5,5">
                                <telerik:RadGridView MaxWidth="1130"
                                                     BorderBrush="{x:Null}"
                                                     BorderThickness="0"
                                                     CanUserSelect="True"
                                                     Foreground="Black"
                                                     ItemsSource="{Binding IdentificationDetail,
                                                                           Mode=OneWay}"
                                                     Style="{StaticResource RadGridViewStandard}">
                                    <telerik:RadGridView.Resources>
                                        <Style x:Key="GridViewToggleButtonColumnStyle"
                                               TargetType="telerik:GridViewCell">
                                            <Setter Property="Padding" Value="0,0,0,0" />
                                        </Style>
                                    </telerik:RadGridView.Resources>
                                    <telerik:RadGridView.Columns>
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding Number}"
                                                                    Header="Identification Number" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding IdentificationType}"
                                                                    Header="Identification Type" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding StateCode}"
                                                                    Header="State" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding CountryCode}"
                                                                    Header="Country" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding IsRevoked}"
                                                                    Header="Revoked" />
                                        <telerik:GridViewDataColumn DataMemberBinding="{Binding UpdateDate}"
                                                                    Header="Last Update" />
                                    </telerik:RadGridView.Columns>
                                </telerik:RadGridView>
                            </StackPanel>
                        </telerik:RadTabItem>
                    </telerik:RadTabControl>
                </DataTemplate>
            </telerik:RadGridView.RowDetailsTemplate>
        </telerik:RadGridView>
    </StackPanel>

  2. Stephen
    Stephen avatar
    21 posts
    Member since:
    Dec 2013

    Posted 25 Jun 2015 Link to this post

    I found another post about a similar issue, which led me to the root cause that you cannot directly access a control inside of a DataTemplate by it's x:Name property.

    So, here is my solution:

    Have a Loaded event on the AlertPerson control in the xaml:

    <controls1:AlertPerson x:Name="controlAlertPerson"
                           Loaded="Alert_Loaded" />

    Then in code behind (VM is my View Model):

    private void Alert_Loaded(object sender, RoutedEventArgs e)
    {
        VM.Alerts = sender as Client.Controls.AlertPerson;
        VM.LoadAlerts();
    }

    Then in my View Model:

    private Client.Controls.AlertPerson _Alerts;

    public Client.Controls.AlertPerson Alerts
     {
                get
                {
                    return _Alerts;
                }
                set
                {
                    _Alerts = value;
                    NotifyPropertyChanged("Alerts");
                }
    }

     and the method to populate it:

    public void LoadAlerts()
    {
         Alerts.AlertSelect("CaseSearch", SearchPersonSelected.PersonId, SearchPersonSelected.BookingBookingId);
    }

  3. DevCraft banner
  4. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 26 Jun 2015 Link to this post

    Hello,

    Generally, on initializations WPF creates fields for each named element in the main name scope (So that you should be able to access the Test1 RadGridVeiw field). However, as DataTemplates work more as blueprints for creating new visual elements, rather that as actual ones, they have name scope of their own. Using the loaded event is a common workaround for accessing DataTemplate element, as soon as they are initialized.

    Regards,
    Ivan Ivanov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top