GridViewDataColumn CellTemplate DataTemplate.Triggers

17 posts, 0 answers
  1. TedK
    TedK avatar
    1 posts
    Member since:
    May 2007

    Posted 08 Apr 2010 Link to this post

    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.

     

  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 12 Apr 2010 Link to this post

    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.
  3. Pritesh
    Pritesh avatar
    2 posts
    Member since:
    Oct 2012

    Posted 24 Oct 2012 Link to this post

    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.
  4. Nick
    Admin
    Nick avatar
    593 posts

    Posted 25 Oct 2012 Link to this post

    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.

  5. Pritesh
    Pritesh avatar
    2 posts
    Member since:
    Oct 2012

    Posted 29 Oct 2012 Link to this post

    Thanks, I worked around the issue by moving all the trigger related logic in View Model.

    I guess a better way to do it.
  6. Jorge
    Jorge avatar
    17 posts
    Member since:
    Oct 2008

    Posted 26 Dec 2012 Link to this post

    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);
  7. Nick
    Admin
    Nick avatar
    593 posts

    Posted 27 Dec 2012 Link to this post

    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.

  8. Mark
    Mark avatar
    15 posts
    Member since:
    Jan 2013

    Posted 23 Jan 2013 Link to this post

    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
  9. Nick
    Admin
    Nick avatar
    593 posts

    Posted 25 Jan 2013 Link to this post

    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.

  10. Mark
    Mark avatar
    15 posts
    Member since:
    Jan 2013

    Posted 29 Jan 2013 Link to this post

    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?
  11. Nick
    Admin
    Nick avatar
    593 posts

    Posted 30 Jan 2013 Link to this post

    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.

  12. Mark
    Mark avatar
    15 posts
    Member since:
    Jan 2013

    Posted 30 Jan 2013 Link to this post

    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

  13. Mark
    Mark avatar
    15 posts
    Member since:
    Jan 2013

    Posted 31 Jan 2013 Link to this post

    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
  14. Tony
    Tony avatar
    1 posts
    Member since:
    Mar 2015

    Posted 28 Jun 2015 Link to this post

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

  15. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1217 posts

    Posted 29 Jun 2015 Link to this post

    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
  16. Lisitsa
    Lisitsa avatar
    16 posts
    Member since:
    Apr 2014

    Posted 02 Jul 2015 in reply to Ivan Ivanov Link to this post

    Hello!

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

  17. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 03 Jul 2015 Link to this post

    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
Back to Top