views:

47

answers:

2

I've a CollectionViewSource as ItemsSource of my DataGrid...on Window.Resources I've this definition:

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

now, I would do the same definition from code, so I've done this:

Dim _cvs as CollectionViewSource = New CollectionViewSource
Dim bindSource = New Binding() With {
        .Path = New PropertyPath("MyObservableCollection"),
        .Mode = BindingMode.OneWay }
_cvs.SetValue(CollectionViewSource.SourceProperty, bindSource)

but I've this error on last statement: "'System.Windows.Data.Binding' is not a valid value for property 'Source'"

What's wrong? How can I do?

The CollectionViewSource don't have the "SetBinding" method..why?

A: 

You don't need to bind a CollectionViewSource in order to make it "bind" automatically; just set the value of the Source property directly:

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

(sorry for my rusty VB.net)

For more info, see the following forum post: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/f44df11b-dfa8-4173-bbc8-051875fce4cc

robertos
I try and it works...but if I change "MyObservebleCollection" on datacontext (clear, add or remove items) the collectionviewsource seems lost the source association.
LukePet
A: 

I solve! ...in this way:

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