How to sort a Listview in a DataTemplate in XAML only?
I have a window with a TabControl that i've bind to a list of objec, I'll called MyItem :
<TabControl Name="MyTabPNL" Background="Gainsboro"
ItemsSource="{Binding 开发者_运维技巧MyItemList, ElementName=WatcherWindow}"
ContentTemplate="{StaticResource tabItemTemplate}">
</TabControl>
This MyItem class has an ObservableCollection, that I want to bind to a Listview, I'm doing this with a DataTemplate. GOAL: I'd like to sort automatically this ObservableCollection in XAML. Usually i would use a CollectionViewSource, but i can't find a way to this... I've tried stuff like that:
<DataTemplate x:Key="tabItemTemplate">
<DataTemplate.Resources>
<CollectionViewSource x:Key='dayList' Source="{Binding MyDayList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="MyDate" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</DataTemplate.Resources>
<Grid >
<ListView ItemsSource="{Binding Source={StaticResource dayList}}" >
<ListView.View>
<GridView x:Name="gridvwDay" >
<GridViewColumn Header="MyDate"
CellTemplate="{StaticResource myCellTemplatePNLDate}"
HeaderContainerStyle="{StaticResource CustomHeaderStyleNeutral}"
Width="70" />
</GridView>
</ListView.View>
</ListView>
</Grid>
But everytime i've got the same error:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=MyDayList; DataItem=null; target element is 'CollectionViewSource' (HashCode=58368655); target property is 'Source' (type 'Object')
I can't find a way to make a link between the dayList in the ListView ItemsSource, and the dayList in the CollectionRessource. Do you guys have an idea?
FYI: pre sorting the ObservableCollection is not doable, because of the nature of the Class i'm using.
Have you tried simply,
<ListView ItemsSource="{StaticResource dayList}">
As per the doc: http://msdn.microsoft.com/en-us/library/ms750950.aspx
You don't need to bind when its just static on the page :)
精彩评论