Reputation: 3604
Here is a style that hides the ListViewItem when bound to a record with a property ALIVE=false, and then sets the color of the same ListViewItem background based on an AlternationIndex:
<Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ALIVE}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value= "LemonChiffon"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
<Setter Property="Background" Value="White"></Setter>
</Trigger>
</Style.Triggers>
</Style>
The intent is to:
What actually happens:
Now the question:
Is it possible to somehow control the AlternationIndex (by telling it to skip rows that should be hidden)?
One alternative (that I don't like):
Instead of hiding rows with a style, filter out the not ALIVE rows from the underlying collection (that gets bound to the ListView)
Upvotes: 2
Views: 901
Reputation: 408
I agree that the most straightforward way would be to filter your data source before binding. However if you are unable to take that course, you could filter your data source in XAML by using an ObjectDataProvider and CollectionViewSource) coupled with your style for alternating row color). See the filtering example here: http://www.galasoft.ch/mydotnet/articles/article-2007081301.aspx
Upvotes: 1
Reputation: 4487
A simple LINQ in the getter of your collection property would save you time instead of an UI oriented solution.. Here is how it would look like..
private ObservableCollection<YourClass> _allDataItems;
//The entire collection
public ObservableCollection<YourClass> AllDataItems
{
get
{
return _allDataItems;
}
set
{
if( _allDataItems == value ) { return; }
_allDataItems = value;
if( _allDataItems != null )
{
_allDataItems.CollectionChangedEvent += ( s, e ) =>
{
RaisePropertyChanged( "DisplayedDataItems" );
};
}
RaisePropertyChanged( "AllDataItems " );
RaisePropertyChanged( "DisplayedDataItems" );
}
}
//The displayed colelction
public ObservableCollection<YourClass> DisplayedDataItems
{
get
{
return AllDataItems.Where( adi => adi.ALIVE == true );
}
}
And you can simply bind DisplayedDataItems to your ListView.
Upvotes: 3