Results 1 to 4 of 4

Thread: WPF Listview alternating row colors

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    WPF Listview alternating row colors

    Anyone here ever do alternating row colors on a listview - not each row but actually BOUND to something in the Observable collection that feeds the listview.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    New Member leo_213's Avatar
    Join Date
    Apr 2014
    Posts
    7

    Re: WPF Listview alternating row colors

    Hiya,

    Actually,you can get this job done like this
    Step 1, Select the range that you want to format.
    Code:
    CellRange dataRange = sheet.AllocatedRange;
    Step 2 Set conditional formation.
    Code:
    ConditionalFormatWrapper format1 = dataRange.ConditionalFormats.AddCondition();
    format1.FirstFormula = "=MOD(ROW(),2)=0";
    format1.FormatType = ConditionalFormatType.Formula;
    format1.BackColor = Color.LightSeaGreen;
    If you still don't know how to do it,plz refer -how to alternate row colors with conditional formatting.
    Hope it helps.

    regards
    leo

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: WPF Listview alternating row colors

    WPF is not EXCEL - which this answer seems to be about - right?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: WPF Listview alternating row colors

    Here's one way to style a listview item/row using Converter and Triggers.
    I assume that you have successfully bind an observable collection object to a listview control.

    Converter class:

    Code:
     public class AddressTargetConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                return (value.ToString().Equals(parameter.ToString()));
            }
    
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    XAML:
    Code:
    <Window.Resources>
            <y:AddressTargetConverter x:Key="AddressTargetConverter" />
        </Window.Resources>
        <Grid>
            <StackPanel>
                <ListView ItemsSource="{Binding PersonCollection}">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Address, Converter={StaticResource AddressTargetConverter}, ConverterParameter = Guam}" Value="True">
                                    <Setter Property="Background" Value="LightBlue"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="140" Header="Name" DisplayMemberBinding="{Binding Name}"  />
                            <GridViewColumn Width="200" Header="Address"  DisplayMemberBinding="{Binding Address}" />
                            <GridViewColumn Width="160" Header="Contact" DisplayMemberBinding="{Binding Contact}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </StackPanel>
        </Grid>
    Note:
    - This will change the row/item color in the listview control if the Address is Guam.
    - Name, Address and Contact are string properties in Person class.
    - add this declaration
    Code:
    xmlns:y="clr-namespace:DemoListview"
    where DemoListview is my project namespace.
    Last edited by KGComputers; Apr 23rd, 2014 at 02:34 AM. Reason: Added Note
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width