1

This is how my Model looks like:

public class MyModel
{
   public string Title { get; set; }
   public string BriefDescription { get; set; }
   public bool IsDirty { get; set; }
}

I have a property in my ViewModel that i am biding ListBox with

    public ObservableCollection<ROCategoryModel> MyCollection { get; set; }

This all works fine but i was wondering if i can refactor this a bit.

I have 2 Listboxes that can use the same Collection but display items based on IsDirty property.

I can create 2 Collections but i think it will be a little overkill. I could be wrong here.

Is there a way i can specify a filter condition in my ListView binding?

This is how my Listview looks like:

        <ListView Name="lvwAvailableCollection" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=MyCollection}" IsSynchronizedWithCurrentItem="True" AlternationCount="2" MaxHeight="300" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="175" Header="Title" DisplayMemberBinding="{Binding Title}" />
                    <GridViewColumn Width="200" Header="Description" DisplayMemberBinding="{Binding BriefDescription}" />
                </GridView>
            </ListView.View>
        </ListView>
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228
Asdfg
  • 11,362
  • 24
  • 98
  • 175

1 Answers1

0

This is doable with CollectionViewSources and a couple of very minor codebehind event handlers. Largely based on Laurent Bugnion's post here:

http://galasoft.ch/mydotnet/articles/article-2007081301.aspx

XAML:

<Window.Resources>
    <ResourceDictionary>
        <CollectionViewSource x:Key="CleanItems" Filter="CleanItems_Filter" Source="{Binding MyCollection}" />
        <CollectionViewSource x:Key="DirtyItems" Filter="DirtyItems_Filter" Source="{Binding MyCollection}" />
    </ResourceDictionary>
</Window.Resources>

<Grid x:Name="LayoutRoot">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <ListView Name="lvwAvailableCollectionClean" Grid.Column="0" ItemsSource="{Binding Source={StaticResource CleanItems}}" IsSynchronizedWithCurrentItem="True" AlternationCount="2" MaxHeight="300" >
        <ListView.View>
            <GridView>
                <GridViewColumn Width="175" Header="Title" DisplayMemberBinding="{Binding Title}" />
                <GridViewColumn Width="200" Header="Description" DisplayMemberBinding="{Binding BriefDescription}" />
            </GridView>
        </ListView.View>
    </ListView>

    <ListView Name="lvwAvailableCollectionDirty" Grid.Column="1" ItemsSource="{Binding Source={StaticResource DirtyItems}}" IsSynchronizedWithCurrentItem="True" AlternationCount="2" MaxHeight="300" >
        <ListView.View>
            <GridView>
                <GridViewColumn Width="175" Header="Title" DisplayMemberBinding="{Binding Title}" />
                <GridViewColumn Width="200" Header="Description" DisplayMemberBinding="{Binding BriefDescription}" />
            </GridView>
        </ListView.View>
    </ListView>

</Grid>

View codebehind:

private void CleanItems_Filter(object sender, System.Windows.Data.FilterEventArgs e)
{
    var item = e.Item as MyModel; 
    if (!item.IsDirty) 
    { 
        e.Accepted = true; 
    } 
    else 
    { 
        e.Accepted = false; 
    }
}

private void DirtyItems_Filter(object sender, System.Windows.Data.FilterEventArgs e)
{
    var item = e.Item as MyModel;
    if (item.IsDirty)
    {
        e.Accepted = true;
    }
    else
    {
        e.Accepted = false;
    }
}
goobering
  • 1,547
  • 2
  • 10
  • 24
  • Does this have to be in Code Behind? Can i not put this in the ViewModel? – Asdfg Jun 12 '15 at 13:52
  • You *can*, but it may be more trouble than it's worth: http://stackoverflow.com/questions/6461826/in-wpf-can-you-filter-a-collectionviewsource-without-code-behind . – goobering Jun 12 '15 at 13:53