为什么CollectionViewSource需要是资源?

时间:2016-06-21 14:41:05

标签: wpf xaml data-binding xaml-binding

我有以下情况,工作正常:

<ListBox ItemsSource="{Binding Source={StaticResource cvs}}">
  <ListBox.Resources>
    <CollectionViewSource x:Key="csv" Source="{Binding Path=PropertyOnViewModel}">
      <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="GroupByProperty" />
      </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
  </ListBox.Resources>
</ListBox>

我尝试将其更改为

<ListBox>
  <ListBox.ItemsSource>
    <Binding>
      <Binding.Source>
        <CollectionViewSource Source="{Binding Path=PropertyOnViewModel}">
          <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="GroupByProperty" />
          </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
      </Binding.Source>
    </Binding>
  </ListBox.ItemsSource>
</ListBox>

这不起作用,它会产生运行时绑定错误

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or 
FrameworkContentElement for target element. BindingExpression:Path=PropertyOnViewModel;
DataItem=null; target element is 'CollectionViewSource' (HashCode=61730159); target
property is 'Source' (type 'Object')

第二个版本有什么问题?

0 个答案:

没有答案