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

Style.Triggers or DataTemplate.Triggers

7 Answers 986 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ryan Abbott
Top achievements
Rank 1
Ryan Abbott asked on 22 Feb 2011, 11:37 PM
I am trying to setup a trigger for when a value in my grid is Null, but I am getting an "Attachable property 'Triggers' was not found in type 'Style' ('DataTemplate')" error in the XAML. I've seen this done before, but I don't know what I'm missing.

Includes:
xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"


Trying with Style.Triggers
<UserControl>
 <UserControl.Resources>
  <Style x:Key="Test" TargetType="TextBlock">
   <Style.Triggers>
    <DataTrigger Binding="{Binding Count}" Value="{x:Null}">
     <Setter Property="Text" Value="~"/>
    </DataTrigger>
   </Style.Triggers>
  </Style>
 </UserControl.Resources>
</UserControl>

Trying with DataTemplate.Triggers:
<telerik:RadGridView.Columns>
 <telerik:GridViewDataColumn>
  <telerik:GridViewDataColumn.CellTemplate>
   <DataTemplate>
    <TextBlock x:Name="txtTest" Text="{Binding Count}" />
     <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding Count}" Value="x:Null">
       <Setter TargetName="txtTest" Property="Text" Value="~"/>
      </DataTrigger>
     </DataTemplate.Triggers>
   </DataTemplate>
  </telerik:GridViewDataColumn.CellTemplate>
 </telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>

7 Answers, 1 is accepted

Sort by
0
Vanya Pavlova
Telerik team
answered on 23 Feb 2011, 03:13 PM
Hello Ryan,

 
The sample below describes the correct approach in this case through TextBlock's Style:

<telerik:RadGridView AutoGenerateColumns="False" x:Name="RadGridView1">
          <telerik:RadGridView.Resources>
              <Style x:Key="StatusBlock" TargetType="TextBlock">
<!--<The following Setter means that the default Text of the TextBlock element is bound to the property of this object>-->
                  <Setter Property="Text" Value="{Binding Path=DigitalSignature}"/>
                  <Style.Triggers>
                      <DataTrigger Binding="{Binding Path=DigitalSignature}" Value="{x:Null}">
                          <Setter Property="TextBlock.Text" Value="~"/>
                      </DataTrigger>
                  </Style.Triggers>
              </Style>
          </telerik:RadGridView.Resources>
          <telerik:RadGridView.Columns>
              <telerik:GridViewDataColumn  Header="Subject" DataMemberBinding="{Binding Subject}"/>
              <telerik:GridViewDataColumn Header="Sender" DataMemberBinding="{Binding Sender}"/>
              <telerik:GridViewDataColumn Header="Size" DataMemberBinding="{Binding Size}"/>
              <telerik:GridViewDataColumn Header="DigitalSignature">
                  <telerik:GridViewDataColumn.CellTemplate>
                      <DataTemplate>
                          <TextBlock Style="{StaticResource StatusBlock}"/>
                      </DataTemplate>
                  </telerik:GridViewDataColumn.CellTemplate>
              </telerik:GridViewDataColumn>
          </telerik:RadGridView.Columns>
      </telerik:RadGridView>


I will be waiting for your response!


Kind regards,
Vanya Pavlova
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ryan Abbott
Top achievements
Rank 1
answered on 23 Feb 2011, 04:41 PM
Thank you for your quick response! However, I'm still getting an error in the XAML that <Style.Triggers> can't be resolved, same with <DataTrigger>. What reference do I need in order to use these?
0
Vanya Pavlova
Telerik team
answered on 23 Feb 2011, 09:05 PM
Hi Ryan,

Considering the fact that this thread is in the forum related to the RadGridView/Silverlight, may you clarify whether you are working with the Silverlight or WPF version of RadGridView? 
This code works for the WPF as expected and you may check the attached project. In case of any misunderstanding it could be great if you could share this info with us.
If you are in Silverlight there are many approach you may use, as to create a converter as shown below:

<telerik:GridViewDataColumn Header="DigitalSignature">
                   <telerik:GridViewDataColumn.CellTemplate>
                       <DataTemplate>
                           <TextBlock Text="{Binding DigitalSignature,Converter={StaticResource con}}"/>
                       </DataTemplate>
                   </telerik:GridViewDataColumn.CellTemplate>
               </telerik:GridViewDataColumn>



Regards,
Vanya Pavlova
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ryan Abbott
Top achievements
Rank 1
answered on 24 Feb 2011, 06:04 PM
Thank you for your help! I was able to get it to work by using a converter. I'm using Silverlight, not WCF, so that's why I posted it in this forum.

0
Himanshu
Top achievements
Rank 1
answered on 20 Feb 2019, 05:01 AM

Hi, I am having trouble with Data template triggers. I want to show images based on values  in my collection. Following is my code it is not working as no image displays on column. Can anyone help me please.

 

<telerik:GridViewDataColumn Header="Status">
                                <telerik:GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Image Name="Image" Width="15" Height="15" />
                                        <DataTemplate.Triggers>
                                            <DataTrigger Binding="{Binding IconHeader}" Value="N">
                                                <Setter TargetName="Image" Property="Source" Value="/Project;component/images/image.png"/>
                                            </DataTrigger>

                                            <DataTrigger Binding="{Binding IconHeader}" Value="Y">

                                                 <Setter TargetName="Image" Property="Source" Value="/Project;component/images/image2.png"/>

                                            </DataTrigger>
                                        </DataTemplate.Triggers>
                                    </DataTemplate>
                                </telerik:GridViewColumn.CellTemplate>
                            </telerik:GridViewDataColumn>

0
Dinko | Tech Support Engineer
Telerik team
answered on 22 Feb 2019, 02:05 PM
Hello Himanshu,

Thank you for the provided code snippet.

I double check this on my side and indeed the DataTriggers are not respected in the CellTemplate. Therefore I have logged feedback item in our Feedback Portal where you can track its progress. As a workaround, you can use Style Triggers instead. Check the following code snippet:
<telerik:GridViewDataColumn Header="Status">
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <Image Name="Image" Width="15" Height="15">
                <Image.Style>
                    <Style TargetType="Image">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IconHeader}" Value="N">
                                <Setter  Property="Source" Value="/Cell_Image;component/images/paste.png"/>
                            </DataTrigger>
 
                            <DataTrigger Binding="{Binding IconHeader}" Value="Y">
                                <Setter  Property="Source" Value="/Cell_Image;component/images/Shapes.png"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>

Hope this workaround will for you. I have updated your Telerik Points for bringing this behavior to our attention.

Regards,
Dinko
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Himanshu
Top achievements
Rank 1
answered on 25 Feb 2019, 06:02 AM
Thank you for the help Dinko, the code snippet you gave works fine. 
Tags
GridView
Asked by
Ryan Abbott
Top achievements
Rank 1
Answers by
Vanya Pavlova
Telerik team
Ryan Abbott
Top achievements
Rank 1
Himanshu
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Share this question
or