Other solutions to implement Alternate Background


We have found three solutions to achieve alternative background scenarios so far. The first one is provided in the previous post. Now we will describe another two.


 


The second solution:


 Derive from ListView, override PrepareContainerForItemOverride method and return the container with the correct background. And in your xaml file, replace ListView with SubListView. When items change, call ListView.Items.Refresh method.


 


The code is as follows.






public class SubListView : ListView


    {


        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)


        {


            base.PrepareContainerForItemOverride(element, item);


            if (View is GridView)


            {


                int index = ItemContainerGenerator.IndexFromContainer(element);


                ListViewItem lvi = element as ListViewItem;


                if (index % 2 == 0)


                {


                    lvi.Background = Brushes.LightBlue;


                }


                else


                {


                    lvi.Background = Brushes.LawnGreen;


                }


            }


        }


    }


 


 


The third solution:


Creat a StyleSelector for ListView.ItemContainerStyleSelector property, in which alternate styles. Reset the containerStyleSeletor property when collection changes.


 


The code is as follows.






public class ListViewItemStyleSelector : StyleSelector


    {


        public override Style SelectStyle(object item, DependencyObject container)


        {


            Style st = new Style();


            st.TargetType = typeof(ListViewItem);


            Setter backGroundSetter = new Setter();


            backGroundSetter.Property = ListViewItem.BackgroundProperty;


            ListView listView = ItemsControl.ItemsControlFromItemContainer(container) as ListView;


            int index = listView.ItemContainerGenerator.IndexFromContainer(container);


            if (index % 2 == 0)


            {


                backGroundSetter.Value = Brushes.LightBlue;


            }


            else


            {


                backGroundSetter.Value = Brushes.LawnGreen;


            }


            st.Setters.Add(backGroundSetter);


            return st;


        }


    }


 

Comments (11)

  1. jasonamh says:

    I enjoy all the samples on extending the ListView Control. I have been exploring for quite a while on how to implement

    a. column freezing as well as

    b. sub columns within a  column, eg.

     |      main column     |

     |sub col.1 | sub col.2 |

    Will there be any working samples on how these can be implemented?

  2. ATC Avalon Team says:

    "a. column freezing as well as"

    you can restyle GridViewColumnHeader’s visual tree(change Template property of GridViewColumnHeader)

    "b. sub columns within a  column"

    you can use GridViewHeaderRowPresenter and GridViewRowPresenter to do it.

    we will blog a sample for it.

    be patient. 🙂

  3. jasonamh says:

    Great! I am looking forward to the sample!

    Though I still couldn’t figure out how  freezing a column can be implemented by just  restyling the GridViewColumnHeader visual tree. When I refer to column freeze, it is like the column freeze in excel.

    Thanks!

  4. jasonamh says:

    Thanks for the hint to use GridViewHeaderRowPresenter and GridViewRowPresenter to implement the Sub columns. I managed to get it working.

    I will appreciate if you can blog a sample of column freeze. It will be interesting to see how this can be implemented.

  5. zhangchenxu says:

    hello,

     i’m a junior chinese-learner,

    i want to create a datagrid viewer like webform-gridview by ListView,and implement display of pagination,how can i achieve that

     thanks

  6. ATC Avalon Team says:

    Hi, jasonamh

    We will try to post a sample of column freeze this week.

    Thanks!

  7. ATC Avalon Team says:

    Hi Zhangchenxu,

    Can you try this? Divide the data items into several pages, and provide the data items to ListVew page by page.

    e.g. Put two buttons (PageUP/PageDown) and a TextBlock (to show the page number) beside ListView. When PageDown button is clicked, set the data items of the next page to ListView.ItemsSoure property, and increase the page number.

    I hope it will help.

  8. zhangchenxu says:

    I will try this,thanks

    I hope you could release more example about how to deal with  database through databinding with ListView

  9. ATC Avalon Team says:

    Thanks for your suggestion.

  10. How To Create Alternate Background In WPF?

  11. piyush_batra says:

    hi

    i am trying to do alternate the background for listview from several days but no success.

    I don’t know XAML. i was trying with c# but it was not working. The solution got compiled but nothing was visible on running the solution.

    Can u post a sample as an attachment here in c# or else provide some pointers on how to do this but plzzz no XAML?

Skip to main content