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

CellTemplate and Bindings

5 Answers 310 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jason Lescalleet
Top achievements
Rank 1
Jason Lescalleet asked on 12 Aug 2009, 06:28 PM
I'm evaluating the RadGridView as a possible data grid for a project I am working on.  One of the things I need the grid to do is to use custom controls to display and edit the contents of cells.  I can do this with the CellTemplate and CellEditTemplate like so:

<controls:GridViewDataColumn Header="Field 1"
  <controls:GridViewDataColumn.CellTemplate> 
    <DataTemplate> 
      <my:CustomControl Text="{Binding Field1}"/> 
    </DataTemplate> 
  </controls:GridViewDataColumn.CellTemplate> 
</controls:GridViewDataColumn> 
The CellEditTemplate works similarly.  This code works fine.  However, it is not very maintainable.  If I have 20 columns in my table that all use those same custom controls, I have to have 20 copies of this code, with only the field name changing.  If I need to change that template, such as to add a property setting to my:CustomControl, I have to edit all 20 of those copies.  

What I would like to do is make that DataTemplate a shared resource.  I have been able to do something like this with some of the other grids I've tried, but I haven't figured out how to do it with RadGridView yet.  Obviously hard-coding {Binding Field1} isn't going to work, but I don't know what binding I need to put in there to make it bind to whatever the GridViewDataColumn is bound to.  I know I can bind a column to a field by doing the following:

<controls:GridViewDataColumn Header="Field 1" DataMemberBinding="{Binding Field1}"/> 

This works as well, if I don't want to use my custom controls.  I'm just not sure how to write the DataTemplates in such a way that they bind to whatever that DataMemberBinding is set to.

If this is possible, please let me know so that I can continue my evaluation of the control.

Thanks,

Jason


5 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 18 Aug 2009, 04:33 PM
Hello Jason,

You can use the magic of attached behaviors to achieve your scenario. I have prepared a small example to illustrate how this can be implemented. Please find it in the attached archived file.

Sincerely yours,
Stefan Dobrev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
MiddleTommy
Top achievements
Rank 1
answered on 17 Nov 2009, 12:25 AM
I cant open the Example. So I recreated the Example and imported the files. I cant find System.Windows.Interactivity.dll
I really need to make re-usable custom CellEditors or DataColumns any help would be much appreciated.
0
Vlad
Telerik team
answered on 17 Nov 2009, 06:39 AM
Hi,

System.Windows.Interactivity.dll can be found in the attached project Binaries folder. I've tried the example and everything worked fine - you can check the same updated with Q3 binaries.

Best wishes,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
MiddleTommy
Top achievements
Rank 1
answered on 17 Nov 2009, 01:26 PM
Thanks
I didnt notice the binaries.
When I say I cant open the project I mean an error message pops up saying

"The project file '...' cant be opened

The project type is not supported by this installation"

I am using VS 2008 Professional. Any idea why it is not supported?
------------------EDIT------------------
I found it. The project is a silverlight project (I just assumed it would be a WPF project in a WPF Forum)
0
Usame Esendir
Top achievements
Rank 1
answered on 02 Mar 2010, 08:28 AM
It doesn't work with progress bar, because ValueProperty of progress bar is in BaseType so you have to change the code in

DataMemberBindingAttachedBehavior


return (DependencyProperty)this.AssociatedObject.GetType().GetField(name, BindingFlags.Public | BindingFlags.Static).GetValue(null); 

to

return (DependencyProperty)this.AssociatedObject.GetType().BaseType.GetField(name, BindingFlags.Public | BindingFlags.Static).GetValue(null); 

Hope it helps,
Usame Esendir
Tags
GridView
Asked by
Jason Lescalleet
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
MiddleTommy
Top achievements
Rank 1
Vlad
Telerik team
Usame Esendir
Top achievements
Rank 1
Share this question
or