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

Generic CellEditTemplateSelector

5 Answers 215 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 05 Sep 2011, 10:36 AM
Hi support team,

I trying to use a special DataTemplate for each data type of column in my grid:
e.g. a DataPicker for Dates, a string editor for strings, ..

I try to achive this by using a style selector, but my problem is that I can't bind the editor to the data of the cell/column.
All the examples  I found always bind a Template to a specific column but not the column of the templated cell.

If I bind the template to a specific column "MyDataColA" like this, it works.
<inh:CellEditTemplateSelector x:Key="cellEditTemplateSelector">
   <inh:CellEditTemplateSelector.DateTimeEditStyle>
      <DataTemplate>
         <telerik:RadDatePicker SelectedValue="{Binding MyDataColA, Mode=TwoWay}"/>
      </DataTemplate>
   </inh:CellEditTemplateSelector.DateTimeEditStyle>
</inh:CellEditTemplateSelector>

But how to bind to the same column as the templated cell?

Is there a way to do this?

Regards
Thomas

5 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 05 Sep 2011, 11:41 AM
Hi Thomas,

Actually, by default the editor of GridViewDataColumn will depend on the type of the corresponding property and a RadDatePicker will be displayed when editing DateTime values for example. Furthermore, a CellEditTemplateSelector is used only for a single instance of a cell, you cannot define one CellEditTemplateSelector for all columns in the grid. May you clarify what is the purpose of trying to do so ? 

 

Greetings,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Thomas
Top achievements
Rank 1
answered on 05 Sep 2011, 04:47 PM
Hi Maya,

I'm trying to define a set of 'special' column editor's e.g. a special editor for date-time, in a MVVM Environment.
The idea is to define this editors once as a template and select the current needed editor in a selector.
Like this (simplified) based on some criteria (might be data type only or data type and column name):
public class CellEditTemplateSelector : DataTemplateSelector {
   public override DataTemplate SelectTemplate(object item, DependencyObject container){
      GridViewCell cell = container as GridViewCell;
      if(cell != null) {
         GridViewBoundColumnBase col = cell.Column as GridViewBoundColumnBase;
         if(col != null) {
            if(col.DataType == typeof(string)) {
               return StringEditStyle;
            } else if(col.DataType == typeof(DateTime)) {
               return DateTimeEditStyle;
            }
         }
      }
      return null;
   }
   public DataTemplate StringEditStyle { get; set; }
   public DataTemplate DateTimeEditStyle { get; set; }
}

As the style selector is responsible for picking the right editor,
the editor could not be bound to specific column. So the binding syntax:
<telerik:RadDatePicker SelectedValue="{Binding MyDataColA
is not suitable in this case.
The question is, is there a syntax to bind this editor to the current column source, the editor
is applied for. (Something like "RelativeBinding").

Or if I'm doing something total strange, is there a better way of doing this?

Regards
Thomas
0
Maya
Telerik team
answered on 07 Sep 2011, 10:12 AM
Hello Thomas,

Actually, I am a bit confused here as to why you are trying to do this since RadGridView will do it automatically for you. May you clarify a bit on this, why do you need to handle such a scenario for defining editors for difference columns ? 
 

Best wishes,
Maya
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Thomas
Top achievements
Rank 1
answered on 09 Sep 2011, 07:26 AM
Hello Maya,

the example with the simple string or DateTime editor was just a example. What I'm really trying is to have special editors for some kind of data, e.g. a editor that allows to pick data that's being retrieved by a sub query from the database with special filter options (not a simple dropdown list) or a very special input format that's not representable with a 'masked' editor, ...

Hope this helps to understand what was the aim of this.

Regards
Thomas
0
Maya
Telerik team
answered on 14 Sep 2011, 04:53 PM
Hello Thomas,

What you may do in this case is to create the template in xaml and handle it in the code-behind by replacing the binding with the corresponding property. This can be done in AutoGeneratingColumn event.
Another way to go is to use FrameworkElementFactory and define the corresponding template depending on the type of the underlying property. 
 

Regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Maya
Telerik team
Thomas
Top achievements
Rank 1
Share this question
or