2008-12-15 52 views
0

我目前有一個HasChanges屬性的集合(集合中的每個對象也有它自己的HasChanges屬性),集合是我的CollectionViewSource的源。如何將數據綁定到collectionviewsource後面的集合的屬性?

當我嘗試將CollectionViewSource後面的集合的HasChanges屬性綁定到我的自定義控件之一時,它綁定到當前選定對象的HasChanges屬性,而不是CollectionViewSource源集合的HasChanges屬性。有沒有一種方法可以明確地告訴綁定查看集合對象而不是集合中的對象?

我的代碼看起來是這樣的:

<Window x:Class="CollectionEditWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:Local="clr-namespace:My.Local.Namespace;assembly=My.Local.Namespace"> 
    <Window.Resources> 
     <CollectionViewSource x:Name="CVS" x:Key="MyCollectionViewSource" /> 
    </Window.Resources> 

<Local:MyCustomControl HasChanges="{Binding HasChanges, Source={StaticResource 
         MyCollectionViewSource}}"> 
<!-- Code to set up the databinding of the custom control to the CollectionViewSource--> 
</Local:MyCustomControl> 
</Window> 

感謝。

回答

2

當您綁定到CollectionViewSource你得到一個CollectionView,其中有一個SourceCollection屬性,您可以用它來獲取集合的CollectionViewSource後面,像這樣:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
    <Grid.Resources> 
     <x:Array x:Key="data" Type="{x:Type sys:String}"> 
      <sys:String>a</sys:String> 
      <sys:String>bb</sys:String> 
      <sys:String>ccc</sys:String> 
      <sys:String>dddd</sys:String> 
     </x:Array> 
     <CollectionViewSource x:Key="cvsData" Source="{StaticResource data}"/> 
    </Grid.Resources> 
    <StackPanel> 
     <ListBox ItemsSource="{Binding Source={StaticResource cvsData}}"/> 
     <TextBlock Text="{Binding Source={StaticResource cvsData}, Path=Length, StringFormat='{}Length bound to current String = {0}'}"/> 
     <TextBlock Text="{Binding Source={StaticResource cvsData}, Path=SourceCollection.Length, StringFormat='{}Length bound to source array = {0}'}"/> 
    </StackPanel> 
</Grid>