CollectionViewSource 绑定到单例中的 属性

CollectionViewSource to be bind to a property in a singleton

我有这么一个单身狗class

public class Sample
{
    private static readonly Lazy<Sample> lazy =
    new Lazy<Sample>(() => new Sample());

    private ObservableCollection<SampleGroup> _groups;

    public ObservableCollection<SampleGroup> Groups
    {
        get { return _groups; }
    }

}

我通过这个把Groups属性绑定到ListView

 <Window.Resources>
    <!-- Data Source For Binding-->
    <CollectionViewSource x:Key="SampleGroups" Source="{Binding Groups}" />
 </Window.Resources>
 ...
 <ListView x:Name="GroupNameListView" 
                                  ItemsSource="{Binding Source={StaticResource SampleGroups}}"
                                  SelectedIndex="0"  SelectionChanged="GroupNameListView_SelectionChanged" >
  ....

为了完成这项工作,我需要在后面的代码中添加 this.DataContext= Sample.Instance

我可以在 <Window.Resources> 部分指定这个 DataContext 吗?因为我想添加另一个具有不同 DataContextCollectionViewSource

您可以直接绑定单例class,如下所述。

<CollectionViewSource x:Key="SampleGroups" Source="{Binding Source={x:Static local:Sample.Instance}, Path=Groups}" />