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

[Solved] [URGENT] Need to change the DataTemplate but get an exception

1 Answer 173 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michele
Top achievements
Rank 2
Michele asked on 08 Sep 2010, 10:02 AM
Hello,
I'm creating a gridview dinamically, I've defined in the App.Resource a DataTemplate as :

<DataTemplate x:Name="hyperLinkTemplate">
<ContentPresenter>
<HyperlinkButton x:Name="hlClick" Content="{Binding}" Click="hlClick_Click"></HyperlinkButton>
</ContentPresenter>
</
DataTemplate>

In the codebehind of the XAML where I add the column I do :

foreach (Colonna colonna in header)
               {
                   GridViewDataColumn c = new GridViewDataColumn();
 
                   c.Header = colonna.N;
                   string columName = string.Format("Colonne[{0}].V", count++);
                   c.DataMemberBinding = new System.Windows.Data.Binding(columName);
                   c.UniqueName = columName;
                   c.DataType = Type.GetType(colonna.T);
 
                   c.CellTemplateSelector = App.Current.Resources["utilityCellTemplateSelector"] as DataTemplateSelector;
                    
                   if (c.DataType == typeof(DateTime))
                   {
                       c.DataFormatString = "{0:dd/MM/yyyy}";
                       c.SortMemberPath = columName;
                       c.SortingState = SortingState.Ascending;
                   }
                   gvContent.Columns.Add(c);
               }

The UtilityCellTemplateSelector is defined as :

public class UtilityCellTemplateSelector : DataTemplateSelector
  {
      public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
      {
          var cell = container as GridViewCell;
 
          if (cell != null && cell.DataColumn is GridViewDataColumn)
          {
              var column = cell.DataColumn as GridViewDataColumn;
 
              string memberPath = column.DataMemberBinding.Path.Path;
 
              System.Windows.DataTemplate dataTemplate = App.Current.Resources["hyperLinkTemplate"] as System.Windows.DataTemplate;
 
             var ar = dataTemplate.FindChildByType<HyperlinkButton>();
              return dataTemplate;
          }
       
          return null;
      }
  }

when I pass the var ar .... I got :  Reference is not a valid visual DependencyObject.

I need to access to the hyperlinkbutton since I need to specify the parameters to open the detail...
Thanks

Paolo

1 Answer, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 08 Sep 2010, 03:18 PM
Hi Paolo,

You cannot call FindChildByType<T>() method for a DataTemplate even that DataTemplate is a DependencyObject, because DataTemplate indeed is not visual. You have to call this method on the FrameworkElement which is a result of DataTemplate.LoadContent() method.
I hope this helps.

Greetings,
Nedyalko Nikolov
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
Tags
GridView
Asked by
Michele
Top achievements
Rank 2
Answers by
Nedyalko Nikolov
Telerik team
Share this question
or