2012-08-29 34 views
0

當在我的Windows Phone項目 我有一個列表框從XML排序列表框項目點擊按鈕

<ListBox x:Name="listBox02"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
       <StackPanel Margin="20" > 
        <TextBlock Text="{Binding Namee}"/> 
        <TextBlock Text="{Binding Examinoo}"/> 
        <TextBlock Text="{Binding Statuss}"/> 
        <!--<TextBlock Text="{Binding Idd}"/>--> 
       </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

數據綁定使用

XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml"); 
var filteredData = 
    from c in loadedCustomData.Descendants("Mathima") 
    where c.Attribute("Id").Value == "1" 
    select new Iatriki() { 
        Namee = "Ονομα: " + c.Attribute("Name").Value, 
        Examinoo = "Εξάμηνο: " + c.Attribute("Examino").Value, 
        Idd = c.Attribute("Id").Value, 
        Statuss = c.Attribute("Status").Value, 
      }; 

listBox02.ItemsSource = filteredData; 

我想通過「Examinoo」對列表進行排序,當點擊一個按鈕

這裏是按鈕的事件處理程序:

private void btn_sort_Click(object sender, RoutedEventArgs e) 
{ 
    listBox02.Items.SortDescriptions.Add(
       new SortDescription("Content", ListSortDirection.Descending)); 

} 

我收到以下錯誤:

System.Windows.Controls.ItemCollection' does not contain a definition for 'SortDescriptions' and no extension method 'SortDescriptions' accepting a first argument of type 'System.Windows.Controls.ItemCollection' could be found (are you missing a using directive or an assembly reference?)`

+0

你有裝配參考'PresentationFramework.dll'? – elyashiv

+0

不!我在哪裏可以找到它? – Gino

+0

我正在Windows Phone應用程序工作。我找到了PresentationFramework.dll,但是當我嘗試添加它時,出現一條消息:「windows手機項目只能與Windows Phone Assemblies一起使用」 – Gino

回答

2

對於同樣的問題,我下面這種方法

中的XDocument線後,取出你的列表框行

listBox02.ItemsSource = filteredData; //remove this line and add the following 2 lines 

ObservableCollection<Iatriki> myCollection = new ObservableCollection<Iatriki>(filteredData); 
listBox02.ItemsSource = myCollection; 

,然後在按鈕請點擊

listBox02.ItemsSource = myCollection.OrderBy(item => item.Examinoo); 
+0

Thunk nkchandra!我會嘗試你的建議! – Gino

+0

就像我說的我是完全新手。你能告訴我,我把第一個代碼放在哪裏?謝謝! – Gino

+0

我已經這樣做了,但在「新的ObservableCollection (filteredData);」出現一個錯誤:'System.Collections.ObjectModel.ObservableCollection '不包含帶1個參數的構造函數。「任何想法?再次請求! – Gino