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

How to Bind a CellTemplate?

9 Answers 264 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ville
Top achievements
Rank 1
Ville asked on 19 Jun 2010, 01:31 AM
Good afternoon everyone,

I would like to know how I can bind a DataTemplate to a CellTemplate in runtime based on the data.

I have use the following XAML code without success:


 

 

 

 

 

<Grid.Resources>

 

 

 

 

 

 

 

 

 

<DataTemplate x:Key="viewButtonTemplate">

 

 

 

 

 

 

 

 

 

<telerik:RadButton Margin="0 0 0 0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"

 

 

 

 

 

 

 

 

HorizontalAlignment="Right" Content="View" Click="RadButtonView_Click"/>

 

 

 

 

 

 

 

 

 

</DataTemplate>

 

 

 

 

 

 

 

 

 

<DataTemplate x:Key="editButtonTemplate">

 

 

 

 

 

 

 

 

 

<telerik:RadButton Margin="0 0 0 0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"

 

 

 

 

 

 

 

 

HorizontalAlignment="Right" Content="Edit" Click="RadButtonEdit_Click"/>

 

 

 

 

 

 

 

 

 

</DataTemplate>

 

 

 

 

 

 

 

 

 

<DataTemplate x:Key="deleteButtonTemplate">

 

 

 

 

 

 

 

 

 

<telerik:RadButton Margin="0 0 0 0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"

 

 

 

 

 

 

 

 

HorizontalAlignment="Right" Content="Delete" Click="RadButtonDelete_Click"/>

 

 

 

 

 

 

 

 

 

</DataTemplate>

 

 

 

 

 

 

 

 

 

</Grid.Resources>


 

<

 

 

telerikGridView:RadGridView Name="radGridViewAuditHistory" Margin="5,5,5,5" AutoGenerateColumns="False" AlternateRowBackground="Azure" AlternationCount="2"

 

 

 

 

 

 

 

 

IsReadOnly="True" ShowGroupPanel="False" IsBusy="True" CanUserDeleteRows="True" ScrollMode="Deferred">

 

 

 

 

 

 

 

 

 

<telerikNavigation:RadContextMenu.ContextMenu>

 

 

 

 

 

 

 

 

 

<telerikNavigation:RadContextMenu Opened="RadContextMenu_Opened" ItemClick="RadContextMenu_ItemClick">

 

 

 

 

 

 

 

 

 

<telerikNavigation:RadContextMenu.Items>

 

 

 

 

 

 

 

 

 

<telerikNavigation:RadMenuItem Header="Edit" />

 

 

 

 

 

 

 

 

 

</telerikNavigation:RadContextMenu.Items>

 

 

 

 

 

 

 

 

 

</telerikNavigation:RadContextMenu>

 

 

 

 

 

 

 

 

 

</telerikNavigation:RadContextMenu.ContextMenu>

 

 

 

 

 

 

 

 

 

<telerikGridView:RadGridView.Columns>

 

 

 

 

 

 

 

 

 

<telerikGridView:GridViewDataColumn CellTemplate="{Binding ButtonType}"/>

In the code behind I do the following:

 

 

 

 

 

 

 

 

bt = facilityAuditInfo.Resources[

 

"editButtonTemplate"] as DataTemplate;

And then bt later is include in my class with INotifyPropertyChanged

What am I doing wrong?

Thanks in advance

 

9 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 21 Jun 2010, 06:52 AM
Hello,

 You can use CellTemplate property for desired column to achieve your goal. For example:

grid.Columns[3].CellTemplate = facilityAuditInfo.Resources["editButtonTemplate"] as DataTemplate;


Kind regards,
Vlad
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
0
Ville
Top achievements
Rank 1
answered on 21 Jun 2010, 04:51 PM
Right but how do I accomplish this since based on the data per row the button could be one or another?

Thanks
0
Accepted
Pavel Pavlov
Telerik team
answered on 21 Jun 2010, 05:12 PM
Hi Giuliano,

In this case the CellTemplateSelector of the column would be handy.
You can  see this in action in this online example.  The example demonstrates how to switch templates depending on your data.

Sincerely yours,
Pavel Pavlov
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
0
Ville
Top achievements
Rank 1
answered on 21 Jun 2010, 07:18 PM
Thank you, that's exactly what I was looking for.

Do you know an easy way to get the DataTemplate from XAML instead of doing this?

template = (DataTemplate)XamlReader.Load(

 

 

 

@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">

 

<telerik:RadButton xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""

HorizontalContentAlignment=""Center"" VerticalContentAlignment=""Center""

Content=""View"" Click=""RadButtonEdit_Click""/>

</DataTemplate>"

 

 

);

 

0
Vlad
Telerik team
answered on 22 Jun 2010, 06:11 AM
Hello,

 You can define both templates are resources and get desired resource in CellTemplateSelector.

Greetings,
Vlad
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
0
Ville
Top achievements
Rank 1
answered on 22 Jun 2010, 06:25 PM
Thanks for your response again, so you mean that if I define the resources in the XAML page, then I can refer to those resources like this, right?

if

 

 

(obj.Is_Remote_Entry)

 

{

 

    template = myPage.gridFacilityAuditInfoLayoutRoot.Resources[

 

"editButtonTemplate"] as DataTemplate;

 

}

 

 

 

else

 

 

{

    template = myPage.gridFacilityAuditInfoLayoutRoot.Resources["viewButtonTemplate"] as DataTemplate;

 

}

I want to make sure I am in the right path because if I try the following, I get and error that tells me that I can't declare event handlers:

 

 

 

template = (

 

DataTemplate)XamlReader.Load(

 

 

 

 

@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">

 

<telerik:RadButton xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"" Content=""Edit"" Click=""RadButtonEdit_Click""/>

</DataTemplate>"

 

 

 

);

Is there a way around it?

Thank you

 

 

0
Vlad
Telerik team
answered on 23 Jun 2010, 06:41 AM
Hello,

 Parsing XAML with events is not a trivial task - it will be better to declare your templates statically.

Greetings,
Vlad
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
0
Ville
Top achievements
Rank 1
answered on 23 Jun 2010, 05:03 PM
So, is it possible or not?
0
Marcos Sampaio
Top achievements
Rank 1
answered on 23 Jun 2010, 08:59 PM
Hello Giuliano,

I think I have what you need.

Firstable create your TemplateSelector class like this:
public class Edit : DataTemplateSelector  
    {  
        public override DataTemplate SelectTemplate(object item, DependencyObject container)  
        {  
            var parametro = item as Parametro;  
            if ((parametro == null) || (parametro.TipoRegistro == 0))  
                return this.EmptyCell;  
 
            if ((parametro.ListaSelecao != null) && (parametro.ListaSelecao.Count > 0))  
                return this.ComboBox;  
 
            var IsMasked = parametro.Mascara.Length > 0;  
 
            switch (parametro.Tipo)  
            {  
                case 'S':  
                    return (IsMasked) ? this.MaskedTextBox : this.TextBox;  
                case 'I':  
                case 'N':  
                    return (IsMasked) ? this.MaskedTextBox : this.Integer;  
                case 'W':  
                case 'O':  
                    return this.Password;  
                case 'D':  
                    return this.DateTime;  
                case 'C':  
                case 'T':  
                    return this.Button;  
            }  
            return base.SelectTemplate(item, container);  
        }  
 
        //Props  
        public DataTemplate EmptyCell { getset; }  
        public DataTemplate TextBox { getset; }  
        public DataTemplate MaskedTextBox { getset; }  
        public DataTemplate Integer { getset; }  
        public DataTemplate Password { getset; }  
        public DataTemplate ComboBox { getset; }  
        public DataTemplate Button { getset; }  
        public DataTemplate DateTime { getset; }  
    } 

Then import it to your XAML:
xmlns:templateselector="clr-namespace:Test.TemplateSelector" 

Now just set those properties you've created before and bind them to a StaticResource
<templateselector:Edit x:Key="EditCells" EmptyCell="{StaticResource Dummy}" TextBox="{StaticResource TextBoxEdit}" 
                MaskedTextBox="{StaticResource MaskedTextBoxEdit}" Integer="{StaticResource IntegerEdit}" 
                Password="{StaticResource PasswordEdit}" ComboBox="{StaticResource ComboBoxEdit}" Button="{StaticResource ButtonEdit}" 
                DateTime="{StaticResource DateTimeEdit}" /> 

Done! Now you can do whatever you want to
<DataTemplate x:Name="TextBoxEdit">  
  <TextBox FontSize="11" Loaded="TextBoxEdit_Loaded" Width="Auto" /> 
</DataTemplate> 

Edit:
I forgot to tell you to add it to the column you want the template:
Column.CellEditTemplateSelector = this.Resources["EditCells"as DataTemplateSelector 

Hope this helps,
BR
Marco.
Tags
GridView
Asked by
Ville
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Ville
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Marcos Sampaio
Top achievements
Rank 1
Share this question
or