0

我想確保在列表框中添加的項目按照每個項目的序列號(例如1項,2項,4項, 3項應按照其編號1.2.3 ....... 10)自動訂購。自動篩選/訂購ListBox項目(Windows Phone)

這裏是C#源:

namespace XeroQuiz 
{ 

    public partial class MainPage : PhoneApplicationPage 
    { 
    IsolatedStorageFile Settings1 = IsolatedStorageFile.GetUserStoreForApplication(); 
    MyDataList listobj = new MyDataList(); 

    public MainPage() 
    { 
     InitializeComponent(); 
     this.Loaded += MainPage_Loaded; 
     this.FavoriteListBox.Visibility = Visibility.Collapsed; 
     if (Settings1.FileExists("MyStoreItems")) 
     { 
      using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open)) 
      { 
       DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList)); 
       listobj = (MyDataList)serializer.ReadObject(fileStream); 

      } 
     } 
     FavoriteListBox.ItemsSource = listobj;//binding isolated storage list data 

     DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(0.5) }; 
     timer.Tick += delegate (object sender, EventArgs e) 
     { 
      // var sortedList = listobj.OrderBy(item => item.AnswerName).ToList(); code is correct but no item is sorted in list. 
      this.FavoriteListBox.ItemsSource = listobj; 
      this.FavoriteListBox.UpdateLayout(); 
     }; 
     timer.Start(); 
    } 


    /**************************************************************************/ 

    private void MainPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     if (Settings1.FileExists("MyStoreItems"))//loaded previous items into list 
     { 
      using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Open)) 
      { 
       DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList)); 
       listobj = (MyDataList)serializer.ReadObject(fileStream); 
      } 
     } 
    } 


    private void FavoriteButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (listobj.Any(l => l.AnswerName == AnswerTextBlock.Text)) 
      return; 
     //var sortedList = listobj.OrderBy(item => item.ToString()).ToList(); 
     listobj.Add(new MyData { AnswerName = AnswerTextBlock.Text }); 

     using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create)) 
     { 
      DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList)); 
      serializer.WriteObject(fileStream, listobj); 

     } 
    } 


    private void FavoriteRemoveButton_Click(object sender, RoutedEventArgs e) 
    { 
     lsitobj.Remove(listobj.FirstOrDefault(l => l.AnswerName == AnswerTextBlock.Text)); 

     using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create)) 
     { 
      DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList)); 
      serializer.WriteObject(fileStream, listobj); 

     } 
    } 
} 



    private void FavoriteListButton_Click(object sender, RoutedEventArgs e) 
    { 
     if (FavoriteListBox.Visibility.Equals(Visibility.Collapsed)) 
     { 
      FavoriteListBox.Visibility = Visibility.Visible;    
     } 
     else if (FavoriteListBox.Visibility.Equals(Visibility.Visible)) 
     { 
      FavoriteListBox.Visibility = Visibility.Collapsed; 
     } 
    } 




    public class MyData 
    { 
     public string AnswerName { get; set; } 
    } 
    public class MyDataList : ObservableCollection<MyData>//for storing mydata class items with type of list 
    { 

    } 


    private void FavoriteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     MyData selecteddata = (sender as ListBox).SelectedItem as MyData;    
     if (selecteddata != null) 
     { 
      FavoritedData.Text = selecteddata.FrequencyName.ToString(); 
      using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MySelectedStoreItem", FileMode.Create)) 
      { 
       DataContractSerializer serializer = new DataContractSerializer(typeof(MyData)); 
       serializer.WriteObject(fileStream, selecteddata); 
      }     
     } 
    } 
} 

這裏是XAML代碼:

<StackPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Top">     
     <ListBox x:Name="FavoriteListBox" Visibility="Collapsed" 
       SelectionChanged="FavoriteListBox_SelectionChanged" 
       HorizontalAlignment="Stretch" 
       VerticalAlignment="Top" Opacity="1" 
       Background="{StaticResource PhoneBackgroundBrush}" Foreground="{StaticResource PhoneForegroundBrush}" 
       Height="300" Width="250"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Visibility="Visible" x:Name="FavoriteListBoxTextBlock" 
           FontSize="35" Foreground="Black" Text="{Binding AnswerName}"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </StackPanel> 

<StackPanel Grid.Row="0" Grid.Column= "0" HorizontalAlignment= "Left" VerticalAlignment= "Top" > 
< Button x:Name= "FavoriteButton" FontFamily= "Segoe MDL2 Assets" 
     Content= "&#xE006;" BorderBrush= "Transparent" FontSize= "28" 
     Foreground= "{StaticResource PhoneForegroundBrush}" 
     Style= "{StaticResource ButtonStyle1}" Click= "FavoriteButton_Click" /> 
</ StackPanel > 

<StackPanel Grid.Row="0" Grid.Column= "2" HorizontalAlignment= "Left" VerticalAlignment= "Top" > 
< Button x:Name= "FavoriteListButton" FontFamily= "Segoe MDL2 Assets" 
     Content= "&#xEA55;" BorderBrush= "Transparent" FontSize= "28" 
     Foreground= "{StaticResource PhoneForegroundBrush}" 
     Style= "{StaticResource ButtonStyle1}" Click= "FavoriteListButton_Click" /> 
</StackPanel> 
+0

什麼是當前輸出看起來像? –

+0

電流輸出根據「當添加項目」,這項目第一次添加會顯示在第一反之亦然 –

回答

1

MainPage構造你有排序的代碼,但您沒有設置內容到列表框,這就是爲什麼它不以排序順序顯示。

var sortedList = listobj.OrderBy(item => item.AnswerName).ToList(); 
this.FavoriteListBox.ItemsSource = sortedList; //you were using listobj, which isn't sorted 

對於FavoriteButton_Click處理程序,您有類似的情況 - 你是分類和排序結果保存到一個新的列表,這並不會影響原來的listobj實例。 OrderBy是LINQ擴展,它不會影響原始實例,因此您只能手動清除並重新添加項目到原始實例。

private void FavoriteButton_Click(object sender, RoutedEventArgs e) 
{ 
    if (listobj.Any(l => l.AnswerName == AnswerTextBlock.Text)) 
     return; 
    //add 
    listobj.Add(new MyData { AnswerName = AnswerTextBlock.Text }); 
    //sort (does not modify the original listobj instance!) 
    var sortedList = listobj.OrderBy(item => item.ToString()).ToList(); 

    //clear and re-add all items in the sorted order 
    listobj.Clear(); 
    foreach(var item in sortedList) 
    { 
     listobj.Add(item); 
    } 

    using (IsolatedStorageFileStream fileStream = Settings1.OpenFile("MyStoreItems", FileMode.Create)) 
    { 
     DataContractSerializer serializer = new DataContractSerializer(typeof(MyDataList)); 
     serializer.WriteObject(fileStream, listobj); 

    } 
} 

另外,作爲一個建議 - 你不必使用MyDataList類型,你可以直接使用ObservableCollection<MyData>無處不在。

總體建議

正如你可以看到這個代碼是已經非常努力維護和保持功能。出於這個原因,我建議你閱讀一些關於設計模式或Windows應用程序開發的書籍,尤其是要了解MVVM模式,數據綁定INotifyPropertyChanged。這些對於構建可維護和穩定的Windows應用程序非常重要。

此外,我認爲這將有助於學習一些更好的C#代碼約定 - 爲更好的可讀性和更一致的變量命名(避免像Settings1,listobj,註釋和代碼結構之類的東西)。這需要時間,但最終結果是值得的努力:-)。

+0

好吧,我會嘗試這個,但現在我需要幫助在這個問題http://stackoverflow.com/questions是秀/ 41074596 /點擊事件應該 - 不觸發時保持事件,執行/ 41076834#41076834這個問題非常小和清晰,但花了幾個小時,做很多事情,但單擊事件觸發 –

+0

大,現在我的列表自動排序1,2,3 ....... 9格式 謝謝:) –