Binding a CollectionViewSource to ObservableCollection

房东的猫 提交于 2019-12-12 08:46:03

问题


Cannot bind the CollectionViewSource

DocProps is a public property

public ObservableCollection<DocProp> DocProps1

DataContext is self

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

This works

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

I cannot wire up a CollectionViewSource to sort This does not even get 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>

How do I bind a CollectionViewSource to a public property?

Thanks


回答1:


It will work this way:

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

This article XAML Binding to a CollectionViewSource property on a ViewModel has some explanations of this behaviour



来源:https://stackoverflow.com/questions/10066075/binding-a-collectionviewsource-to-observablecollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!