根据属性值将ObservableCollection绑定到Listbox

时间:2015-06-12 00:53:30

标签: wpf listview data-binding

这就是我的模型的样子:

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

我的ViewModel中有一个属性,我在ListBox

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

这一切都很好,但我想知道我是否可以重构一下。

我有2个列表框可以使用相同的集合,但显示基于IsDirty属性的项目。

我可以创建2个收藏集,但我认为它会有点矫枉过正。我在这里错了。

我可以在ListView绑定中指定过滤条件吗?

这就是我的Listview的样子:

        <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>

1 个答案:

答案 0 :(得分:0)

这对于CollectionViewSources和一些非常小的代码隐藏事件处理程序是可行的。主要基于Laurent Bugnion的帖子:

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>

查看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;
    }
}