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

GridViewDataColumn CellTemplate DataTemplate.Triggers

16 Answers 1884 Views
GridView
This is a migrated thread and some comments may be shown as answers.
TedK
Top achievements
Rank 1
TedK asked on 08 Apr 2010, 06:09 PM
Why doesn't this work in RadGridView but works fine in standard ListView control?:

 
I have a CellTemplate with DataTemplate.Triggers defined:

<Window.Resources>

 

<DataTemplate x:Key="ActiveGraphic">

 

 

<StackPanel>

 

 

<Control x:Name="icon" Template="{StaticResource addImage}" />

 

 

</StackPanel>

 

 

<DataTemplate.Triggers>

 

 

<DataTrigger Binding="{Binding Path=Enabled}" Value="True">

 

 

<Setter TargetName="icon" Property="Template" Value="{StaticResource addImage}"/>

 

 

</DataTrigger>

 

 

<DataTrigger Binding="{Binding Path=Enabled}" Value="False">

 

 

<Setter TargetName="icon" Property="Template" Value="{StaticResource deleteImage}" />

 

 

</DataTrigger>

 

 

</DataTemplate.Triggers>

 

 

</DataTemplate>

and GridViewDataColumn:
<telerik:GridViewDataColumn Header="Activated" DataMemberBinding="{Binding Path=Enabled}" CellTemplate="{StaticResource ActiveGraphic}" />
DataTemplate.Traggers doesn't fire and Template property never changes from default.

 

16 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 12 Apr 2010, 02:37 PM
Hi TedK,

It seems you have hit a known issue in RadGridVIew. The problem is due to the specific way RadGridVIew loads the cell template. We are already working on the fix. I believe this will take some time as we do not want to introduce major breaking changes. Therefore I am offering  you the following workaround :

Instead of using the CellTemplate property , please use the CellStyle property of the column. Within the cell style set the ContentTemplate property. This should force the cell to apply the template the standard way and triggers would be fine ( tested ).

XAML would be something like :

<Style TargetType="telerik:GridViewCell" x:Key="MyCellStyle">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            ...                         <DataTemplate.Triggers>
                                ...
                            </DataTemplate.Triggers>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
<telerik:RadGridView x:Name="RadGridView1" AutoGenerateColumns="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumnCellStyle="{StaticResource MyCellStyle}"  />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>


Regards,
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
Pritesh
Top achievements
Rank 1
answered on 24 Oct 2012, 06:37 PM
Have you fixed this issue in latest version.  I have exact same issue, working with latest released version.

I have a trigger inside data template that is not working.
0
Nick
Telerik team
answered on 25 Oct 2012, 07:18 AM
Hello Pritesh,

The issue is still present, however you can use the workaround provided by my colleague. 
Instead of using the CellTemplate property , please use the CellStyle property of the column. Within the cell style set the ContentTemplate property. This should force the cell to apply the template the standard way and triggers would be fine ( tested ).

You can track the state of the issue and vote for its resolution here

Greetings,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Pritesh
Top achievements
Rank 1
answered on 29 Oct 2012, 04:05 PM
Thanks, I worked around the issue by moving all the trigger related logic in View Model.

I guess a better way to do it.
0
Jorge
Top achievements
Rank 1
answered on 26 Dec 2012, 07:06 PM
I am also waiting for this resolution. And by the way... There is no CellStyle property available for GridViewColumn.CellTemplate.
How else can I do this? I need to designate a style with a DataTrigger for dynamically changing the cell style.

This does not work. How else can I do this?

 

 

 

 

Style style = new Style(typeof(TextBlock));
style.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Green));
DataTrigger dataTrigger = new DataTrigger() { Binding = new Binding("DateNeededBy") { Converter = new PastDateToBooleanConverter() }, Value = true };
dataTrigger.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Red));
style.Triggers.Add(dataTrigger);
myColumn.CellTemplate.VisualTree.SetValue(TextBlock.StyleProperty, style);

However, this works, but then I have no Style or Triggers:

myColumn.CellTemplate.VisualTree.SetValue(TextBlock.ForegroundProperty, Brushes.Red);
0
Nick
Telerik team
answered on 27 Dec 2012, 08:19 AM
Hello Jorge,

The idea is to use the GridViewColumn.CellStyle property. 

Let me know if you encounter any problem with the implementation!  

Regards,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Mark
Top achievements
Rank 1
answered on 23 Jan 2013, 05:11 PM
Ummm... the example
<telerik:RadGridView x:Name="RadGridView1" AutoGenerateColumns="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumnCellStyle="{StaticResource MyCellStyle}"  />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>


Seems wrong. If it is wrong, could you provide the correct example?
Thank you
0
Nick
Telerik team
answered on 25 Jan 2013, 09:21 AM
Hello Mark,

Here is the correct syntax: 

<telerik:GridViewDataColumn DataMemberBinding="{Binding Property}">
                   <telerik:GridViewDataColumn.CellStyle>
                       <Style />
                   </telerik:GridViewDataColumn.CellStyle>
               </telerik:GridViewDataColumn>


Hope it helps! 

Kind regards,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Mark
Top achievements
Rank 1
answered on 29 Jan 2013, 11:00 PM
Thank you for giving the correct format.

ummm... I guess what I am looking for, is way to set the default look of cells in a RadGridView so I don't have to set it on each column. However I dont know if it means styling cells or rows or columns or the RadGridView itself or ...

The behaviour I have now is this: when I resize columns (i.e make them narrow), the textual data in the cells will wrap and the row will grow to always show all that text. This means that I get rows that are extremely stretched to show the text.

The format I want is this: when I resize columns (to make them narrow), the text will wrap ... to a point, but if there are more than, say, 3 lines needed to show the text, the cell should then get horizontal/vertical scroll bars so the user can access all the text if need be. That way the rows stay as reasonably compact.

I suspect there are several ways of accomplishing this.
1) if there is a cell styling way of doing this, I would like to see it so I have a simple example of styling cells 
2) If there is a row/column formatting way of doing it, I would like to see that too, so I can learn about "styling" rows/colums

Thoughts?
0
Nick
Telerik team
answered on 30 Jan 2013, 04:22 PM
Hi Mark,

We have various examples of how you can customize the Styles/Templates of the Cells and Rows. 

Here are some of them:
http://demos.telerik.com/silverlight/#GridView/Selectors/DataTemplateSelectors/CellTemplateSelector
http://demos.telerik.com/silverlight/#GridView/CustomRowLayout
http://demos.telerik.com/silverlight/#GridView/Selectors/StyleSelectors/RowStyleSelector


You can find the same demos for WPF in your local copy of our Examples. 

Hope this helps! 

All the best,
Nik
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Mark
Top achievements
Rank 1
answered on 30 Jan 2013, 07:06 PM
Thank you for your reply.

Ummm... The examples have value, but I am not sure how they can be used to solve my need.

I am looking for a way to change the default format of most cells in most of my RadGridView (there are some exceptions but I want to define the default look of cells). The examples shown bind to specific data or set the style based on a choice and dont solve the general case.

I am looking to define some thing that when I call:
  <telerik:GridViewDataColumn Header="Product ID" DataMemberBinding="{Binding ProductID}" Width="60" /> 
  <telerik:GridViewDataColumn Header="In Stock" DataMemberBinding="{Binding InStock}" Width="60" /> 
  <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" Width="60" /> 
  <telerik:GridViewDataColumn  ......

in each case the text (i.e ProductID, InStock, Name, etc ) should presented in a TextBox that has a MaxLines or MaxHeight and shows scroll bars if needed (and other attributes that I might want to be common)

I am hoping I am expressing my need correctly

Thank you for you help

0
Mark
Top achievements
Rank 1
answered on 31 Jan 2013, 08:08 PM
Ok I found my answer,

I define a DataTemplate for the grid view cell. The trouble I had was accessing the text data within the template. Finally I see that I can use GridViewCell Value.

Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:GridViewCell}},Path=Value}"
 (not pretty but it works)

Now I can use
<telerik:GridViewDataColumn Header = "Name" DataMemberBinding="Name" CellTemplate={StaticResource TextCellTemplate} />

This also relates to http://www.telerik.com/community/forums/wpf/gridview/regarding-gridviewcell-value-binding.aspx

Thank you
0
Tony
Top achievements
Rank 1
answered on 29 Jun 2015, 12:40 AM

Could you please provide an update on the status of this issue? Thanks, Tony.

0
Ivan Ivanov
Telerik team
answered on 29 Jun 2015, 05:17 AM
Hello Tony,

I am afraid that we had to close this issue, as we did not find an appropriate fix after researching it extensively. I am posting a comment by the colleague, who worked on the issue:

"Indeed, it has been awhile, but we tried to find a solution that will not break the current functionality or introduce performance problems. With WPF DataGrid DataTemplate used as CellTemplate is set directly as ContentTemplate to the cell and all triggers are directly transferred.
However, with RadGridView the content of the cell is set from template.LoadContent() method and is returned from the CreateCellElement method of the column.
One possible scenario for a fix is to change that behavior and apply directly the template as WPF DataGrid does. However, this is a huge breaking change with CreateCellElement method is widely used. The second approach is to leave the code as it is right now, return the element from LoadContent method of DataTemplate and set the ContentTemplate property of the cell to that DataTemplate. This will cause the content to be loaded twice. Which will degrade the performance.

So, as it turns out the solution is either to introduce breaking change, or degrade the performance. Both of them are against our believes and efforts to improve the controls.

Our suggestion is to use the workaround by setting triggers directly to the style. We do hope you understand our concerns."


Please, let us know if the suggested workaround works for you.

Regards,
Ivan Ivanov
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Lisitsa
Top achievements
Rank 1
answered on 02 Jul 2015, 02:14 PM

Hello!

I indeed can set ContentTemplate in CellStyle, but how do I use triggers in CellEditTemplate? 

0
Maya
Telerik team
answered on 03 Jul 2015, 12:56 PM
Hello Andrey,

The approach should be the same - set the triggers to the style of the editing element. For example, if the editing element is TextBox:
<Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsChampion}" Value="True">
                    <Setter Property="Background" Value="BlueViolet"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>


Regards,
Maya
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
TedK
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Pritesh
Top achievements
Rank 1
Nick
Telerik team
Jorge
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Tony
Top achievements
Rank 1
Ivan Ivanov
Telerik team
Lisitsa
Top achievements
Rank 1
Maya
Telerik team
Share this question
or