简体   繁体   中英

ObservableCollection not Sorting

I am writing an MVVM application but I am having trouble with an Observable Collection. The ObservableCollection is held in the view model as:

private ObservableCollection<Participant> _initiativeList;
public ObservableCollection<Participant> InitiativeList
{
    get { return _initiativeList; }
    set
    {
        _initiativeList = value;
        OnPropertyChanged("InitiativeList");
    }
}

In the XAML I have a list box :

    <ListBox x:Name="lvInitiativeList"
              DockPanel.Dock="Top"
              ItemsSource="{Binding Source={StaticResource InitiativeListCollection}}"
              ItemTemplate="{StaticResource ResourceKey=ParticipantDisplayPanel}"
              SelectedItem="{Binding Path=SelectedParticipant}"/>

The collectionViewSource it uses is:

<CollectionViewSource Source="{Binding Path=InitiativeList, Mode=OneWay}"
                      x:Key="InitiativeListCollection">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="InitiativeScore" Direction="Descending"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

and the Item Template is:

<DataTemplate x:Key="ParticipantDisplayPanel">
    <uc:ParticipantDisplayPanel/>
</DataTemplate>

the ParticipantDisplayPanel is a UserControl defined as:

<UserControl x:Class="InitiativeList.View.UserControls.ParticipantDisplayPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:InitiativeList.View.UserControls">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Resources\CommonStyles.xaml"/>
                <ResourceDictionary Source="..\Resources\ConverterDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Border BorderBrush="{Binding Path=ActionState, Mode=OneWay, Converter={StaticResource BorderHighlight}}"  
            BorderThickness="3"
            CornerRadius="10"
            Background="{Binding Path=Kind, Mode=OneTime, Converter={StaticResource KindColor}}">
        <StackPanel Orientation="Horizontal"
                    Width="450"
                    Height="50"
                    Background="Transparent">
            <Border BorderBrush="Black"
                    BorderThickness="2"
                    Width="50"
                    Height="50"
                    CornerRadius="25"
                    Background="#FF8040">
            <TextBlock x:Name="tbInitiative"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       FontSize="30"
                       Text="{Binding Path=InitiativeScore, 
                                      Mode=OneWay, 
                                      Converter={StaticResource InitiativeScore}}"/>
            </Border>
            <TextBlock x:Name="tbName"
                       Background="Transparent"
                       Width="250"
                       Height="25"
                       FontFamily="Ariel"
                       FontWeight="Bold"
                       FontSize="20"
                       Text="{Binding Path=Name, Mode=OneWay}"/>
            <TextBlock x:Name="Condition"
                       Background="Transparent"
                       Width="100"
                       Height="25"
                       Text="{Binding Path=Condition, Mode=OneWay}"/>
            <Border BorderBrush="Black"
                    BorderThickness="2"
                    Width="50"
                    Height="50"
                    CornerRadius="25"
                    Background="{Binding Path=Healthiness, Mode=OneWay, 
                                                              Converter={StaticResource HPBackground}}">
                <TextBlock x:Name="tbHitPoints"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           FontSize="25"
                           Text="{Binding Path=CurrentHitPoints, Mode=OneWay}"/>
            </Border>
        </StackPanel>
    </Border>
</UserControl>

When I add items to the observable collection the items appear in the ListBox, and when I update properties of the elements the ParticipantDisplayPanels update to reflect the changed data. However the collection does not sort in response to these changes, unless I specifically call OnPropertyChanged for the list, something I was expecting to be done as part of the function of the ObservableCollection, or at least through the Set code on the InitiativeList Property. It feels wrong to have to pepper the OnPropertyChanged calls throughout the code. Am I doing something wrong? all my searches on the internet haven't turned up an example of an Observable Collection that I can relate to how I've written my code.

Any pointers as to what is going wrong would be much appreciated.

I want to implement some custom sorting on Observable collection and I did it like below and then bind:

ItemsSource="{Binding CollectionView}"...

List<SortDescription> SortDescriptions = new List<SortDescription>();

SortDescriptions.Add(new SortDescription("Field1", ListSortDirection.Ascending));
SortDescriptions.Add(new SortDescription("Field2", ListSortDirection.Ascending));
SortDescriptions.Add(new SortDescription("Field3", ListSortDirection.Ascending));
SortDescriptions.Add(new SortDescription("Field4", ListSortDirection.Ascending));


CollectionViewSource collectionViewSource = new CollectionViewSource();
public ICollectionView CollectionView
{
    get
    {
        collectionViewSource.Source = <YourObservableCollection>;
        if (SortDescriptions != null)
        {
            foreach (SortDescription sd in SortDescriptions)
            {
                collectionViewSource.View.SortDescriptions.Add(sd);
            }
        }

        collectionViewSource.View.Refresh();
        return collectionViewSource.View;        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM