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
- Anonymous
March 28, 2006
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? - Anonymous
March 28, 2006
"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. :) - Anonymous
March 29, 2006
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! - Anonymous
March 31, 2006
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. - Anonymous
April 05, 2006
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 - Anonymous
April 05, 2006
Hi, jasonamh
We will try to post a sample of column freeze this week.
Thanks! - Anonymous
April 05, 2006
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. - Anonymous
April 06, 2006
I will try this,thanks
I hope you could release more example about how to deal with database through databinding with ListView - Anonymous
April 06, 2006
Thanks for your suggestion. - Anonymous
May 30, 2007
How To Create Alternate Background In WPF? - Anonymous
July 24, 2007
hii 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?