我有一个 CollectionViewSource 作为 ItemsSourceDataGrid 。在 Window.Resources 我有这个定义:

<CollectionViewSource x:Key="ItemsPoolCollectionView"
     Source="{Binding Path=MyObservableCollection, Mode=OneWay}" />

现在,我想从代码中生成相同的定义,所以我这样做了:
Dim _cvs as CollectionViewSource = New CollectionViewSource
Dim bindSource = New Binding() With {
        .Path = New PropertyPath("MyObservableCollection"),
        .Mode = BindingMode.OneWay }
_cvs.SetValue(CollectionViewSource.SourceProperty, bindSource)

但我在最后一条语句中有这个错误:



怎么了?我怎样才能做到这一点?

最佳答案

我解决! ...这样:

      Dim _cvs as CollectionViewSource = New CollectionViewSource
      Dim bindSource = New Binding() With {
              .Source = Me.DataContext
              .Path = New PropertyPath("MyObservableCollection"),
              .Mode = BindingMode.OneWay }
      BindingOperations.SetBinding(cvs, CollectionViewSource.SourceProperty, bindSource)

关于wpf - CollectionViewSource 源属性上的集合绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4031498/

10-12 02:49