How to Customize a RowStyle

5 posts, 1 answers
  1. Vitor
    Vitor avatar
    53 posts
    Member since:
    Mar 2016

    Posted 30 Mar Link to this post

    Hello Telerik,

    I am trying to customize a RowStyle, depending on what comes from the database.

    My first attempt is very simple, but I am getting this error when running

    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
    Additional information: 'DataGridRow' TargetType does not match type of element 'GridViewRow'.

     

    the error makes sense, since telerik GridView is something different than a DataGrid

    but I am trying to implement the same approach.

    I think it will be more clear when you see my code below:

    <telerik:RadGridView Name="RadGridView"  ItemsSource="{Binding View}" Style="{StaticResource VitorStyle}"   AutoGenerateColumns="False">

    <Style x:Key="VitorStyle" TargetType="telerik:RadGridView" >
             
            <Setter Property="RowStyle">
                <Setter.Value>
                    <Style TargetType="DataGridRow">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Name}" Value="Anastacia Santana">
                                <Setter Property="Background" Value="Red"></Setter>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Name}" Value="Ana Taylor">
                                <Setter Property="Background" Value="DarkOrchid"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

    As a kickoff test, I would like all Anastacia Santana rows to be RED and all Ana Taylor to be DarkOrchid.

    My following tests should envolve a bit wether it is true or false, or a sum result wether it is positive or negative.

     

    Can you please show me the best way to achieve this?

    as a side note, I would like to keep my XAML as clean as possible. Thus the use of the resource file

     

    Thank you very much!

     

  2. Answer
    Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    280 posts

    Posted 30 Mar Link to this post

    Hello Vitor,

    The Style that you have defined should target "telerik:GridViewRow". Eventually, you can directly set the Background Property. Please, try the following Style:

    <Style x:Key="VitorStyle" TargetType="telerik:GridViewRow" >
                <Style.Triggers>
                   <DataTrigger Binding="{Binding Name}" Value="Anastacia Santana">
                        <Setter Property="Background" Value="Red"></Setter>
                    </DataTrigger>
                   <DataTrigger Binding="{Binding Name}" Value="Ana Taylor">
                        <Setter Property="Background" Value="DarkOrchid"></Setter>
                    </DataTrigger>
                </Style.Triggers>
    </Style>
    And setting the "RowStyle" property of RadGridView:

    <telerik:RadGridView Name="RadGridView"  ItemsSource="{Binding View}"  RowStyle="{StaticResource VitorStyle}"   AutoGenerateColumns="False">

    Another approach is to use a RowStyleSelector. Please, review the following article from our documentation page - RadGridView - RowStyleSelectors

    Regards,
    Stefan Nenchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Vitor
    Vitor avatar
    53 posts
    Member since:
    Mar 2016

    Posted 01 Apr in reply to Stefan Nenchev Link to this post

    If i use the RowStyle property of the RadGridView, i get this unpleasing error

     

    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
    Additional information: 'RadGridView' TargetType does not match type of element 'GridViewRow'.

     

    I will give a try to the method of the link you quoted and let you know in a moment

  5. Vitor
    Vitor avatar
    53 posts
    Member since:
    Mar 2016

    Posted 01 Apr in reply to Vitor Link to this post

    The RowStyleSelector WORKS.

     

    thank you!

  6. Stefan Nenchev
    Admin
    Stefan Nenchev avatar
    280 posts

    Posted 01 Apr Link to this post

    Hello Vitor,

    I am glad to hear that the RowStyleSelector is convenient for you. As for the RowStyle of the RadGridView, please make sure that the Style you have created targets "telerik:GridViewRow" and not "telerik:RadGridView".

    Regards,
    Stefan Nenchev
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready