CollectionViewSource 源属性上的集合绑定

发布于 2024-09-29 16:25:43 字数 755 浏览 3 评论 0原文

我有一个 CollectionViewSource 作为我的 DataGridItemsSource。在 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”不是属性“Source”的有效值

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

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

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

now, I would like to produce 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 the last statement:

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

What's wrong? How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2024-10-06 16:25:43

我解决! ...这样:

      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)

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)
谜兔 2024-10-06 16:25:43

您不需要绑定 CollectionViewSource 即可使其自动“绑定”;只需直接设置 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文