4

我有一个CollectionViewSourceItemsSourceDataGrid. 在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)

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

'System.Windows.Data.Binding' is not a valid value for property 'Source'

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

4

2 回答 2

4

我解决!...这样:

      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)
于 2010-10-27T13:10:23.767 回答
1

您无需绑定 aCollectionViewSource即可使其自动“绑定”;只需直接设置 Source 属性的值:

Dim _cvs as CollectionViewSource = New CollectionViewSource
_cvs.Source = Me.MyObservableCollection

(对不起我生锈的 VB.net)

有关详细信息,请参阅以下论坛帖子: http ://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f44df11b-dfa8-4173-bbc8-051875fce4cc

于 2010-10-27T09:47:39.333 回答