• <small id='byu8A'></small><noframes id='byu8A'>

    <tfoot id='byu8A'></tfoot>

    <i id='byu8A'><tr id='byu8A'><dt id='byu8A'><q id='byu8A'><span id='byu8A'><b id='byu8A'><form id='byu8A'><ins id='byu8A'></ins><ul id='byu8A'></ul><sub id='byu8A'></sub></form><legend id='byu8A'></legend><bdo id='byu8A'><pre id='byu8A'><center id='byu8A'></center></pre></bdo></b><th id='byu8A'></th></span></q></dt></tr></i><div id='byu8A'><tfoot id='byu8A'></tfoot><dl id='byu8A'><fieldset id='byu8A'></fieldset></dl></div>

        <bdo id='byu8A'></bdo><ul id='byu8A'></ul>
      1. <legend id='byu8A'><style id='byu8A'><dir id='byu8A'><q id='byu8A'></q></dir></style></legend>
      2. 自动过滤/排序 ListBox 项目 (Windows Phone)

        时间:2023-10-07

          <tbody id='bJLWW'></tbody>
        <legend id='bJLWW'><style id='bJLWW'><dir id='bJLWW'><q id='bJLWW'></q></dir></style></legend>
            <bdo id='bJLWW'></bdo><ul id='bJLWW'></ul>
              <tfoot id='bJLWW'></tfoot>

              1. <i id='bJLWW'><tr id='bJLWW'><dt id='bJLWW'><q id='bJLWW'><span id='bJLWW'><b id='bJLWW'><form id='bJLWW'><ins id='bJLWW'></ins><ul id='bJLWW'></ul><sub id='bJLWW'></sub></form><legend id='bJLWW'></legend><bdo id='bJLWW'><pre id='bJLWW'><center id='bJLWW'></center></pre></bdo></b><th id='bJLWW'></th></span></q></dt></tr></i><div id='bJLWW'><tfoot id='bJLWW'></tfoot><dl id='bJLWW'><fieldset id='bJLWW'></fieldset></dl></div>
                • <small id='bJLWW'></small><noframes id='bJLWW'>

                  本文介绍了自动过滤/排序 ListBox 项目 (Windows Phone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想确保添加到我的列表框中的项目按照每个项目的序列号升序排列(例如 1 项目、2 项目、4 项目、3 项目应该根据其编号自动排序1.2.3.......10).

                  I want to ensure, that the items added in my list box are ordered in ascending order according to the serial number of each item (e.g 1 item, 2 item, 4 item, 3 item should be automatically order according to its number 1.2.3.......10).

                  这里是 C# 源代码:

                  Here is the C# source:

                  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 代码:

                  Here is the XAML code:

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

                  推荐答案

                  MainPage 构造函数中你有排序代码,但你没有将内容设置到列表框,这就是为什么它没有按排序顺序显示.

                  In the MainPage constructor you have the sort code, but you don't set the content to the list box, which is why it didn't display in sorted order.

                  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 扩展,所以你只能手动清除和重新添加项目到原实例.

                  For the FavoriteButton_Click handler, you have a similar situation - you were sorting and saving the sorted results into a new list, which did not affect the original listobj instance. OrderBy is a LINQ extension which does not affect the original instance, so you can only clear and re-add the items to the original instance manually.

                  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.

                  Also as a suggestion - you don't have to use MyDataList type, you can directly use ObservableCollection<MyData> everywhere.

                  正如您所见,这段代码越来越难以维护和保持功能.出于这个原因,我建议您阅读一些有关设计模式或 Windows 应用程序开发的书,尤其是了解 MVVM 模式、数据绑定INotifyPropertyChanged.这些对于构建可维护和稳定的 Windows 应用程序非常重要.

                  As you can see this code is getting quite hard to maintain and keep functional. For that reason I suggest you to read some book on design patterns or Windows app development, especially to learn about MVVM pattern, data-binding and INotifyPropertyChanged. These are quite essential in building maintainable and stable Windows applications.

                  另外,我认为学习一些更好的 C# 代码约定会有所帮助 - 通过更一致的变量命名(避免像 Settings1listobj 之类的东西)、注释来获得更好的可读性和代码结构.这需要时间,但最终结果值得付出努力:-).

                  Also I think it would be helpful to learn some better C# code conventions - for better readability with more consistent variable naming (avoiding things like Settings1, listobj), commenting and code structure. It takes time but the end result is well worth the effort :-) .

                  这篇关于自动过滤/排序 ListBox 项目 (Windows Phone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将列表框的选定值显示为标签 - 多个值 下一篇:无法使用 TabStops 使 ListBox 中的项目正确对齐

                  相关文章

                  <tfoot id='ArMki'></tfoot>

                    <bdo id='ArMki'></bdo><ul id='ArMki'></ul>
                  <i id='ArMki'><tr id='ArMki'><dt id='ArMki'><q id='ArMki'><span id='ArMki'><b id='ArMki'><form id='ArMki'><ins id='ArMki'></ins><ul id='ArMki'></ul><sub id='ArMki'></sub></form><legend id='ArMki'></legend><bdo id='ArMki'><pre id='ArMki'><center id='ArMki'></center></pre></bdo></b><th id='ArMki'></th></span></q></dt></tr></i><div id='ArMki'><tfoot id='ArMki'></tfoot><dl id='ArMki'><fieldset id='ArMki'></fieldset></dl></div>
                • <legend id='ArMki'><style id='ArMki'><dir id='ArMki'><q id='ArMki'></q></dir></style></legend>

                    1. <small id='ArMki'></small><noframes id='ArMki'>