access datatemplate from code-behind

4 posts, 0 answers
  1. cielo valdoz
    cielo valdoz avatar
    33 posts
    Member since:
    Dec 2009

    Posted 03 Feb 2011 Link to this post

    Hi,

    I want to access the gridview columns from code-behind so i can set their values dynamically. In my code, I have to set the source of image(imgLogo, imgFlag) and text of a textblock(txtLocation). When selected at RadTabControl, the Gridview from DataTemplate will load, but they are in UserControl.Resources and im not sure how to access them.

    <UserControl.Resources>
          <MyDs:CompanyTypeContext x:Key="Code"/>
          <riaControls:DomainDataSource x:Name="CompanyTypeDDS">
              <riaControls:DomainDataSource.DomainContext>
                  <MyDs:CompanyTypeContext/>
              </riaControls:DomainDataSource.DomainContext>
          </riaControls:DomainDataSource>
          <riaControls:DomainDataSource x:Name="CountryListDDS">
              <riaControls:DomainDataSource.DomainContext>
                  <MyDs:CountryListContext/>
              </riaControls:DomainDataSource.DomainContext>
          </riaControls:DomainDataSource>
          <riaControls:DomainDataSource x:Name="ClientListDDS">
              <riaControls:DomainDataSource.DomainContext>
                  <MyDs:ClientListContext/>
              </riaControls:DomainDataSource.DomainContext>
          </riaControls:DomainDataSource>
          <DataTemplate x:Key="TabItemTemplate" x:Name="ClientContent">
              <Button Command="{Binding ElementName=CompanyTypeDDS, Path=DataContext.StepChangeCommand}" CommandParameter="{Binding}">
                  <Button.Content>
                      <StackPanel Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Width="700">
                          <Grid>
                              <Grid.RowDefinitions>
                                  <RowDefinition />
                                  <RowDefinition />
                              </Grid.RowDefinitions>
                              <TextBlock Text="{Binding Name}" Grid.Row="0" x:Name="IndustryCode"/>
                              <telerik:RadGridView Grid.Row="1" ItemsSource="{Binding ElementName=ClientListDDS, Path=Data}">
                                  <telerik:RadGridView.Columns>
                                      <telerik:GridViewImageColumn ImageHeight="62" ImageWidth="90" x:Name="imgLogo" />
                                      <telerik:GridViewImageColumn ImageHeight="11" ImageWidth="16" x:Name="imgFlag"/>
                                      <telerik:GridViewDataColumn>
                                          <telerik:GridViewDataColumn.CellTemplate>
                                              <DataTemplate>
                                                  <Grid>
                                                      <Grid.RowDefinitions>
                                                          <RowDefinition />
                                                          <RowDefinition />
                                                      </Grid.RowDefinitions>
                                                      <TextBlock Margin="2" Grid.Row="0" Text="{Binding NameOnReferenceList}" FontWeight="Bold" />
                                                      <TextBlock Margin="2" Grid.Row="1" x:Name="txtLocation" />
                                                  </Grid>
                                              </DataTemplate>
                                          </telerik:GridViewDataColumn.CellTemplate>
                                      </telerik:GridViewDataColumn>
                                      <telerik:GridViewDataColumn DataMemberBinding="{Binding RefDescription}" />
                                  </telerik:RadGridView.Columns>
                              </telerik:RadGridView>
                          </Grid>
                      </StackPanel>
                  </Button.Content>
              </Button>
          </DataTemplate>
      </UserControl.Resources>
      <Grid x:Name="LayoutRoot" Background="White" Width="800">
          <Grid.RowDefinitions>
              <RowDefinition Height="33*" />
              <RowDefinition Height="267*" />
          </Grid.RowDefinitions>
           <telerik:RadComboBox Grid.Row="0" SelectedValuePath="Code" ItemsSource="{Binding ElementName=CountryListDDS, Path=Data}" DisplayMemberPath="Country" x:Name="ddlCountry" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalAlignment="Left" HorizontalContentAlignment="Left" />
       
          <telerik:RadTabControl Grid.Row="1" Name="RadTabIndustry" VerticalAlignment="Top" TabOrientation="Vertical" TabStripPlacement="Left" OverflowMode="Wrap" VerticalContentAlignment="Top"
                                 ItemsSource="{Binding ElementName=CompanyTypeDDS, Path=Data}" DisplayMemberPath="Name" Align="Right" 
                                 ContentTemplate="{StaticResource TabItemTemplate}" HorizontalAlignment="Left" HorizontalContentAlignment="Left" IsTextSearchEnabled="False">
          </telerik:RadTabControl>
            
      </Grid>

    Private Sub GetReferenceList(ByVal sEGSId As String, ByVal lngCodeCountryLocal As Long, _
                                       ByVal lngCodeCountry As Long, ByVal flagLocal As Boolean, _
                                       ByVal lngLang As Long, Optional ByVal intProductGroup As Integer = -1, _
                                       Optional ByVal intCompanyType As Integer = -1)
          Dim oDDSClientList As DomainDataSource = Me.Resources("ClientListDDS")
          Dim parSessionID As New Parameter
          Dim parCodeCountryLocal As New Parameter
          Dim parCodeCountry As New Parameter
          Dim parForLocal As New Parameter
          Dim parLang As New Parameter
          Dim parProductGroup As New Parameter
          Dim parCompanyType As New Parameter
          parSessionID.ParameterName = "sessionId"
          parCodeCountryLocal.ParameterName = "codeCountryLocal"
          parCodeCountry.ParameterName = "codeCountry"
          parForLocal.ParameterName = "forLocal"
          parLang.ParameterName = "lang"
          parProductGroup.ParameterName = "productGroup"
          parCompanyType.ParameterName = "companyType"
          parSessionID.Value = sEGSId
          parCodeCountryLocal.Value = lngCodeCountryLocal
          parCodeCountry.Value = lngCodeCountry
          parForLocal.Value = flagLocal
          parLang.Value = lngLang
          parProductGroup.Value = intProductGroup
          parCompanyType.Value = intCompanyType
          oDDSClientList.QueryParameters.Clear()
          oDDSClientList.QueryName = "GetClientList"
          oDDSClientList.QueryParameters.Add(parSessionID)
          oDDSClientList.QueryParameters.Add(parCodeCountryLocal)
          oDDSClientList.QueryParameters.Add(parCodeCountry)
          oDDSClientList.QueryParameters.Add(parForLocal)
          oDDSClientList.QueryParameters.Add(parLang)
          oDDSClientList.QueryParameters.Add(parProductGroup)
          oDDSClientList.QueryParameters.Add(parCompanyType)
          oDDSClientList.Load()
      End Sub

    Private Sub RadTabIndustry_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RadTabIndustry.SelectionChanged
        _intCodeCountryLocal = ddlCountry.SelectedValue
        _intCodeCountry = ddlCountry.SelectedValue
        subShowReferenceDetails1()
    End Sub

    Thanks,
    Cielo
  2. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 08 Feb 2011 Link to this post

    Hello cielo valdoz,

    Depending on the source from where the image source and textbox information are acquired  you can one of the following approaches:


    - If the data is static (e.g. one image needs to be assigned each time)  you can define the data as static resource and refer it by key.

    - if the data is derived from the datacontext you can define binding and add converter if needed to return the correct data/imagesource


    Greetings,
    Tsvyatko
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  3. DevCraft banner
  4. cielo valdoz
    cielo valdoz avatar
    33 posts
    Member since:
    Dec 2009

    Posted 09 Feb 2011 Link to this post

    Hi Tsvyatko ,

    The data came from database and I'm binding it dynamically in code-behind. I changed the approach now on fetching data, now using a WCF services and LINQ-To-SQL. The contents of RadTabControl is populating automatically and its content was inside the DataTemplate. Now my problem is how to access the control inside the DataTemplate.

    I have to access the StackPanel <StackGlobal> from codebehind. So if there's no row returned from Gridview, i will hide the StackPanel.

    This is the function to show/hide from code-behind.

    Private Sub grdClientList2_Loaded(ByVal sender As System.Object, ByVal e As System.EventArgs)
        grdClientList2 = sender
     
        If grdClientList2.HasItems = False Then
            grdClientList2.Visibility = Visibility.Collapsed
     
            'I have to create a function to get the StackGlobal from DataTemplate then hide it.
     
        Else
            grdClientList2.Visibility = Visibility.Visible
        End If
    End Sub


    Contents of RadTabControl which is located at DataTemplate.
    <UserControl.Resources>
           <DataTemplate x:Key="TabItemTemplate" x:Name="ClientContent">
               <StackPanel Orientation="Horizontal" VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="785">
                   <Grid HorizontalAlignment="Left" VerticalAlignment="Top">
                       <Grid.RowDefinitions>
                           <RowDefinition />
                           <RowDefinition />
                       </Grid.RowDefinitions>
                       <telerik:RadGridView telerik:StyleManager.Theme="Vista" x:Name="grdClientList"  ItemsSource="{Binding lst1}" Grid.Row="0" RowLoaded="grdClientList_RowLoaded" AutoGenerateColumns="False" ShowColumnHeaders="False" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" DataLoaded="grdClientList_Loaded" VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="785" ShowGroupPanel="False" CanUserDeleteRows="False" CanUserInsertRows="False" CanUserSortColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserFreezeColumns="False" IsFilteringAllowed="False" IsHitTestVisible="True" IsTabStop="True" RowIndicatorVisibility="Collapsed" CanUserSelect="False" IsScrolling="False">
                           <telerik:RadGridView.Columns>
                               <telerik:GridViewDataColumn  Width="100">
                                   <telerik:GridViewDataColumn.CellTemplate>
                                       <DataTemplate>
                                           <Grid>
                                               <Image x:Name="imgLogo" Width="90" Height="62" />
                                           </Grid>
                                       </DataTemplate>
                                   </telerik:GridViewDataColumn.CellTemplate>
                               </telerik:GridViewDataColumn>
                               <telerik:GridViewDataColumn  Width="30">
                                   <telerik:GridViewDataColumn.CellTemplate>
                                       <DataTemplate>
                                           <Grid>
                                               <Image x:Name="imgFlag" Width="16" Height="11"/>
                                           </Grid>
                                       </DataTemplate>
                                   </telerik:GridViewDataColumn.CellTemplate>
                               </telerik:GridViewDataColumn>
                               <telerik:GridViewDataColumn  Width="215">
                                   <telerik:GridViewDataColumn.CellTemplate>
                                       <DataTemplate>
                                           <Grid>
                                               <Grid.RowDefinitions>
                                                   <RowDefinition />
                                                   <RowDefinition />
                                               </Grid.RowDefinitions>
                                               <TextBlock Margin="2" Text="{Binding NameOnReferenceList}" Grid.Row="0" FontWeight="Bold" x:Name="txtCompany" TextWrapping="Wrap" />
                                               <TextBlock Margin="2" Grid.Row="1" x:Name="txtLocation" />
                                           </Grid>
                                       </DataTemplate>
                                   </telerik:GridViewDataColumn.CellTemplate>
                               </telerik:GridViewDataColumn>
                               <telerik:GridViewDataColumn TextWrapping="Wrap" Width="440">
                                   <telerik:GridViewDataColumn.CellTemplate>
                                       <DataTemplate>
                                           <Grid>
                                               <TextBlock TextWrapping="Wrap" x:Name="txtDescription" Text="{Binding RefDescription}" />
                                           </Grid>
                                       </DataTemplate>
                                   </telerik:GridViewDataColumn.CellTemplate>
                               </telerik:GridViewDataColumn>
                           </telerik:RadGridView.Columns>
                       </telerik:RadGridView>
                       <StackPanel Grid.Row="1" x:Name="StackGlobal">
                           <Grid>
                               <Grid.RowDefinitions>
                                   <RowDefinition />
                                   <RowDefinition />
                               </Grid.RowDefinitions>
                               <TextBlock Text="Rest of the world" FontWeight="Bold" FontSize="12" />
                               <telerik:RadGridView telerik:StyleManager.Theme="Vista" x:Name="grdClientList2" ItemsSource="{Binding lst2}" Grid.Row="3" RowLoaded="grdClientList2_RowLoaded" AutoGenerateColumns="False" ShowColumnHeaders="False" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" DataLoaded="grdClientList2_Loaded" VerticalAlignment="Stretch" HorizontalAlignment="Left" Width="785" ShowGroupPanel="False" RowIndicatorVisibility="Collapsed" CanUserDeleteRows="False" CanUserFreezeColumns="False" CanUserInsertRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSelect="False" CanUserSortColumns="False" IsFilteringAllowed="False" IsTabStop="True" IsScrolling="False" UseLayoutRounding="True" EnableColumnVirtualization="True" EnableRowVirtualization="True">
                                   <telerik:RadGridView.Columns>
                                       <telerik:GridViewDataColumn Width="100" Header="Global">
                                           <telerik:GridViewDataColumn.CellTemplate>
                                               <DataTemplate>
                                                   <Grid>
                                                       <Image x:Name="imgLogo" Width="90" Height="62" />
                                                   </Grid>
                                               </DataTemplate>
                                           </telerik:GridViewDataColumn.CellTemplate>
                                       </telerik:GridViewDataColumn>
                                       <telerik:GridViewDataColumn Width="30">
                                           <telerik:GridViewDataColumn.CellTemplate>
                                               <DataTemplate>
                                                   <Grid>
                                                       <Image x:Name="imgFlag" Width="16" Height="11"/>
                                                   </Grid>
                                               </DataTemplate>
                                           </telerik:GridViewDataColumn.CellTemplate>
                                       </telerik:GridViewDataColumn>
                                       <telerik:GridViewDataColumn Width="215" TextWrapping="Wrap">
                                           <telerik:GridViewDataColumn.CellTemplate>
                                               <DataTemplate>
                                                   <Grid>
                                                       <Grid.RowDefinitions>
                                                           <RowDefinition />
                                                           <RowDefinition />
                                                       </Grid.RowDefinitions>
                                                       <TextBlock Margin="2" Text="{Binding NameOnReferenceList}" Grid.Row="0" FontWeight="Bold" x:Name="txtCompany" TextWrapping="Wrap" />
                                                       <TextBlock Margin="2" Grid.Row="1" x:Name="txtLocation" />
                                                   </Grid>
                                               </DataTemplate>
                                           </telerik:GridViewDataColumn.CellTemplate>
                                       </telerik:GridViewDataColumn >
                                       <telerik:GridViewDataColumn TextWrapping="Wrap" Width="440">
                                           <telerik:GridViewDataColumn.CellTemplate>
                                               <DataTemplate>
                                                   <Grid>
                                                       <TextBlock TextWrapping="Wrap" x:Name="txtDescription" Text="{Binding RefDescription}" />
                                                   </Grid>
                                               </DataTemplate>
                                           </telerik:GridViewDataColumn.CellTemplate>
                                       </telerik:GridViewDataColumn>
                                   </telerik:RadGridView.Columns>
                               </telerik:RadGridView>
                           </Grid>
                       </StackPanel>
                        
                   </Grid>
               </StackPanel>
           </DataTemplate>
     
       </UserControl.Resources>

    This is the RadTabControl.

    <Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Top" Width="785" HorizontalAlignment="Left" Height="800">
           <Grid.RowDefinitions>
               <RowDefinition Height="12*" />
               <RowDefinition Height="278*" />
           </Grid.RowDefinitions>
           <telerik:RadComboBox ItemTemplate="{StaticResource DropDownTemplate}" Grid.Row="0" x:Name="ddlCountry" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalAlignment="Left" HorizontalContentAlignment="Left" />
           <telerik:RadTabControl telerik:StyleManager.Theme="Vista" Grid.Row="1" Name="RadTabIndustry" VerticalAlignment="Stretch" TabOrientation="Horizontal" TabStripPlacement="Top" OverflowMode="Wrap" VerticalContentAlignment="Stretch"
                                  Align="Left" ContentTemplate="{StaticResource TabItemTemplate}"
                                  HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" IsTextSearchEnabled="False" Width="785">
           </telerik:RadTabControl>
           
       </Grid>

    Thank,
    Cielo Valdoz
    Programmer
  5. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 14 Feb 2011 Link to this post

    Hi cielo valdoz,

    I can suggest go with the binding/Converter option:
    In your scenario you might need to bind the StackGlobal visibility to lst2 property and add converter that returns Collapsed if lst2.Count==0.

    <StackPanel Grid.Row="1" x:Name="StackGlobal" Visibility="{Binding lst2,Converter=YourConverterKey}">

    The convert method in your converter will look like this:
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((ICollection)value).Count>0 ? Visibility.Visible : Visiblity.Collapsed;
    }


    Kind regards,
    Tsvyatko
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Back to Top