Question

I have this xaml

  <Grid>
    <ItemsControl ItemsSource="{Binding Path=TempSol}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding }"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Grid>

and this is my observable collection

    private ObservableCollection<double> _dTempSol = new ObservableCollection<double> { 3.21, -5.41, -15.81, -21.69, -21.70, -12.60, -6.41, -0.06, 5.42, 13.32, 14.12, 7.55, 0 };
    public ObservableCollection<double> TempSol
    {
        get { return _dTempSol; }
    }

What I end up with is the exact same thing I've put in the OC but I would like to have it from the biggest one to the smaller and I don't know how, or if I need to format my OC or if there's any method to do so

Edit TO be precise what I see is

3.21 -5.41 -15.81 -21.69 -21.70 -12.60 -6.41 -0.06 5.42 13.32 14.12 7.55 0

WHat I want to end up with is

14.12 13.42 7.55 5.42 3.21 0 -0.06 -5.41 -6.41 -12.60 -15.81 -21.69 -21.70
Was it helpful?

Solution

Have CollectionViewSource which will be wrapper over your actual collection and add SortDescription in it. Bind ItemsSource with CollectionViewSource instead.

Add CVS as a resource and bind like this:

<Grid>
  <Grid.Resources>
     <CollectionViewSource x:Key="SortedCollection"
          xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
          Source="{Binding TempSol}">
         <CollectionViewSource.SortDescriptions>
            <scm:SortDescription Direction="Descending"/>
         </CollectionViewSource.SortDescriptions>
     </CollectionViewSource>
  </Grid.Resources>
  <ItemsControl ItemsSource="{Binding Source={StaticResource SortedCollection}}">
     <ItemsControl.ItemTemplate>
        <DataTemplate>
           <TextBlock Text="{Binding }"/>
        </DataTemplate>
     </ItemsControl.ItemTemplate>
   </ItemsControl>
</Grid>

Refer to the detailed explanation of using CollectionViewSource from MSDN here.

OTHER TIPS

var ds = new List<double>{
3.21 -5.41 -15.81 -21.69 -21.70 -12.60 -6.41 -0.06 5.42 13.32 14.12 7.55 0};


_dTempSol = new ObservableCollection(ds.OrderByDescending(d));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top