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

Set Cell Template Image code behind

7 Answers 139 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Colin Wright
Top achievements
Rank 2
Colin Wright asked on 08 Feb 2012, 11:54 AM
If I have the following column in a radgrid, could someone help me with code behind on how to set the image source programtically based on a column value?

<telerik:GridViewColumn Header="Notes" Width="50">

<telerik:GridViewColumn.CellTemplate>

<DataTemplate>

<telerik:RadButton Name="btnObj" Height="25" Width="25" Click="btnObj_Click">

<telerik:RadButton.Content>

<Image Name="imgNotes" Source="/Images/notes.png" Height="16" Width="16" />

</telerik:RadButton.Content>

 

</telerik:RadButton>

</DataTemplate>

</telerik:GridViewColumn.CellTemplate>

7 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 08 Feb 2012, 12:16 PM
Hi Colin,

 

The recommended approach to achieve this result is through defining CellTemplateSelector.
Have you checked our DataTemplateSelector demos?
The same is applicable to WPF as well. 



Greetings,
Vanya Pavlova
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Colin Wright
Top achievements
Rank 2
answered on 14 Feb 2012, 01:42 PM
Thanks for the link however I am still stuggling. I have implented the following class and added the resources to my page as below. But I cannot see how to set the style to the radribbonbutton that is within my datatemplate of the cell in my grid.

 

 

  public class NotesButtonStyle : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            if (item is OBJECTIVEALL)
            {
                OBJECTIVEALL row = item as OBJECTIVEALL;

                if (row.ManagerNotes == null && row.EmployeeNotes == null)
                    return NoNotes;
                else
                    return HasNotes;
                   

            }
            return null;
        }

        public Style HasNotes { get; set; }
        public Style NoNotes { get; set; }
    }

 

 

    <UserControl.Resources>
        <local:RatingMatcherStyle x:Key="ratingMatcherStyle">
            <local:RatingMatcherStyle.RatingMisMatch>
                <Style TargetType="telerik:GridViewCell">
                    <Setter Property="Foreground" Value="Red"></Setter>
                </Style>
            </local:RatingMatcherStyle.RatingMisMatch>
        </local:RatingMatcherStyle>

        <local:NotesButtonStyle x:Key="notesButtonStyle">
            <local:NotesButtonStyle.HasNotes>
                <Style TargetType="telerik:RadRibbonButton">
                    <Setter Property="SmallImage" Value="/Images/notes_empty.png"/>
                </Style>
            </local:NotesButtonStyle.HasNotes>
            <local:NotesButtonStyle.NoNotes>
                <Style TargetType="telerik:RadRibbonButton">
                    <Setter Property="SmallImage" Value="/Images/notes.png"/>
                </Style>
            </local:NotesButtonStyle.NoNotes>
        </local:NotesButtonStyle>
    </UserControl.Resources>

 

 

 

 

 

 

0
Vlad
Telerik team
answered on 14 Feb 2012, 01:57 PM
Hi,

 According your first reply you have RadButton not RadRibbonButton. Can you clarify?

Greetings,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Colin Wright
Top achievements
Rank 2
answered on 14 Feb 2012, 02:01 PM
sorry its a radribbonbutton I am using
0
Colin Wright
Top achievements
Rank 2
answered on 17 Feb 2012, 11:59 AM
Its a RadRibbonButton. Any chance you could advise please?
0
Accepted
Tina Stancheva
Telerik team
answered on 17 Feb 2012, 02:05 PM
Hello Colin,

I believe in your case it would be better to create a converter instead of a StyleSelector. This would allow you to apply a Style on the RadRibbontButton based on your custom logic. I attached a sample project to demonstrate this approach. Please give ti a try and let me know if it works for you.

All the best,
Tina Stancheva
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Colin Wright
Top achievements
Rank 2
answered on 17 Feb 2012, 04:10 PM
You are a genius!! Worked Perfectly many thanks
Tags
GridView
Asked by
Colin Wright
Top achievements
Rank 2
Answers by
Vanya Pavlova
Telerik team
Colin Wright
Top achievements
Rank 2
Vlad
Telerik team
Tina Stancheva
Telerik team
Share this question
or