2015-03-02 46 views
0

我有這樣一個單獨的類CollectionViewSource被綁定到一個屬性在一個單

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,?因爲我想補充另一個CollectionViewSource具有不同DataContext

回答

1

您可以直接綁定單例類,如下所述。

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