How to sort ListView in DataTemplate only in XAML?

I have a window with TabControl that I link to the objec list, I will call MyItem:

    <TabControl Name="MyTabPNL"  Background="Gainsboro"
        ItemsSource="{Binding MyItemList, ElementName=WatcherWindow}"
        ContentTemplate="{StaticResource tabItemTemplate}">
    </TabControl>

There is an ObservableCollection in this MyItem class that I want to associate with a Listview, I do this with a DataTemplate. GOAL: I would like to automatically sort this ObservableCollection in XAML. Normally I would use CollectionViewSource, but I cannot find a way to do this ... I tried things like this:

<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 every time I get the same error:

System.Windows.Data error: 2: Cannot find the Office FrameworkElement or FrameworkContentElement for the target element. BindingExpression: Path = MyDayList; DataItem = NULL; target element 'CollectionViewSource' (HashCode = 58368655); target property "Source" (type "Object")

dayList ListView ItemsSource dayList CollectionRessource. ?

FYI: ObservableCollection , - , .

+3
1

,

<ListView ItemsSource="{StaticResource dayList}">

http://msdn.microsoft.com/en-us/library/ms750950.aspx

, :)

+1

Source: https://habr.com/ru/post/1764139/


All Articles