CollectionViewSource要绑定到单例中的属性

本文关键字:属性 单例中 绑定 CollectionViewSource | 更新日期: 2024-09-22 21:48:13

我有这样一个单例类

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吗,?因为我想添加另一个具有不同CCD_ 7的CCD_。

CollectionViewSource要绑定到单例中的属性

您可以直接绑定singleton类,如下所述。

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