wpf绑定到Observable集合的格式输出

时间:2014-03-01 17:43:58

标签: c# wpf binding formatting

我有这个xaml

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

这是我的可观察集合

    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; }
    }

我最终得到的是我在OC中提出的完全相同的东西,但是我希望从最大的那个到更小的那个,我不知道如何,或者我是否需要格式化我的OC或者如果有任何方法可以这样做

修改 的 准确地说,我看到的是

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

我最终想要的是

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

2 个答案:

答案 0 :(得分:1)

CollectionViewSource包装在您的实际收藏中并在其中添加SortDescription。用CollectionViewSource绑定ItemsSource

将CVS添加为资源并像这样绑定:

<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>

请参阅MSDN here中使用 CollectionViewSource 的详细说明。

答案 1 :(得分:1)

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));
相关问题