将CollectionViewSource绑定到ObservableCollection

时间:2012-04-08 20:06:34

标签: .net binding collectionviewsource

无法绑定CollectionViewSource

DocProps是一个公共财产

public ObservableCollection<DocProp> DocProps1

DataContext是自我

DataContext="{Binding RelativeSource={RelativeSource self}}"

这有效

<ListBox Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=DocProps1}">

我无法连接CollectionViewSource进行排序 这甚至没有得到DocProps1

    <ListBox Grid.Row="0" Grid.Column="0">
        <ListBox.ItemsSource>
            <Binding>
                <Binding.Source>
                    <CollectionViewSource Source="{Binding Path=DocProps1}">
                        <CollectionViewSource.SortDescriptions>
                            <scm:SortDescription PropertyName="Name" />
                        </CollectionViewSource.SortDescriptions>
                    </CollectionViewSource>
                </Binding.Source>
            </Binding>
        </ListBox.ItemsSource>

如何将CollectionViewSource绑定到公共属性?

由于

1 个答案:

答案 0 :(得分:10)

它将以这种方式工作:

<ListBox ItemsSource="{Binding}">
  <ListBox.DataContext>
    <CollectionViewSource Source="{Binding Path=DocProps1}">
    </CollectionViewSource>
  </ListBox.DataContext>    
</ListBox>

本文XAML Binding to a CollectionViewSource property on a ViewModel对此行为有一些解释