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

Reusable Cell Style

2 Answers 237 Views
GridView
This is a migrated thread and some comments may be shown as answers.
JTango
Top achievements
Rank 1
JTango asked on 22 Sep 2008, 04:47 AM
I want to create a number of grids that are bound to objects that have Boolean properties.

I want create a generic style that I can apply to various columns of my choosing that causes a cell to have "tick" if the value is true, but a "cross" if the value is false.

  <Style x:Key="YesNoCellStyle" TargetType="telerik:GridViewCell"
                <Setter Property="Template"
                    <Setter.Value> 
                        <ControlTemplate TargetType="telerik:GridViewCell"
                            <Image x:Name="img" Source="Images/tick.png" 
                                    Width="16" Height="16" /> 
                            <ControlTemplate.Triggers> 
                                <DataTrigger Binding="{Binding}" Value="False"
                                    <Setter TargetName="img" Property="Source" Value="Images/cross.png"/> 
                                </DataTrigger> 
                            </ControlTemplate.Triggers> 
                        </ControlTemplate> 
                    </Setter.Value> 
                </Setter> 
            </Style> 
   
So far I have not been able to accomplish this because the DataContext for the GridViewCell is an entire DataRecord, rather than just the field that I'm binding to that column.

Therefore my DataTrigger requires that I know the FieldName or the FieldPosition in order to retrieve the value; so I have to create a template for each DataObject. E.g.

<DataTrigger Binding="{Binding Path=Data.Type}" Value="False"
 
<DataTrigger Binding="{Binding Path=Data.IsAvailable}" Value="False"
 
<DataTrigger Binding="{Binding Path=Data.IsValid}" Value="False"

What I really want is for the Binding to be boudn to whatever value is in the cell being styled. Does anyone know how to acheieve this.

Regards

Justin

- Sorry if this is a little unclear, let me know if you need clarification.

2 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 23 Sep 2008, 07:11 AM
Hi Innocence18,

There is an example in our QSF section showing how to achieve the desired behavior. Please take a look  at the "GridView/Styling/GridViewCell/Custom Content Template "  example. It displays different national flags in the cell , depending on the cell content , without specifying the column index or the column name in the template.

It benefits from the fact that the GridViewCell is a ContentControl. Thus, setting the appropriate Data Template and binding to the Content property  will the work.

Alternatively, if you prefer to use the  ControlTemplate technique rather than Data Template, you can try  binding to the GridViewCell.Field.Value property.

Let me know if you have troubles with any of these approaches.

All the best,
Pavel Pavlov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
JTango
Top achievements
Rank 1
answered on 23 Sep 2008, 09:34 AM
Thanks Pavel, setting the DataTemplate did the trick.
Tags
GridView
Asked by
JTango
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
JTango
Top achievements
Rank 1
Share this question
or