Wpf 如何将数据绑定到collectionviewsource后面集合上的属性?
我当前有一个集合,其上有一个HasChanges属性(集合中的每个对象也有自己的HasChanges属性),该集合是我的CollectionViewSource的源 当我尝试将CollectionViewSource后面集合的HasChanges属性数据绑定到某个自定义控件时,它绑定到当前选定对象的HasChanges属性,而不是CollectionViewSource的源集合的HasChanges属性。有没有一种方法可以显式地告诉绑定查看集合对象而不是集合中的对象 我的代码如下所示:Wpf 如何将数据绑定到collectionviewsource后面集合上的属性?,wpf,data-binding,Wpf,Data Binding,我当前有一个集合,其上有一个HasChanges属性(集合中的每个对象也有自己的HasChanges属性),该集合是我的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>
谢谢。当您绑定到CollectionViewSource时,您会得到一个,它有一个属性,您可以使用该属性在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>
A.
bb
ccc
dddd