Cross-ListBox在嵌套的ListBox WP7应用程序中选择 [英] Cross-ListBox Selects in a Nested ListBox WP7 App

查看:73
本文介绍了Cross-ListBox在嵌套的ListBox WP7应用程序中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows Phone 7应用程序中嵌套列表框的一个已知问题"是,对于每个父类别,它们各自的子列表框保留其自己的SelectedItems列表.好吧,我遇到了这种情况,这是预期的行为,但是在捕获父级和子级ListBox"选择的列表时遇到了问题.

A known "issue" with nesting ListBoxes in a Windows Phone 7 App is that for each parent category their respective child ListBox retains its own SelectedItems list. Well, I have a situation where this is expected behavior, but I'm having issues capturing both the Parent and Child ListBox selected lists.

当前功能: 1.清单项目 2.清单项目 3.正在加载父子列表框数据 4.父列表框项目的多选可完美运行 5.子列表框项目的多选有效,但不可访问 6.在UI中可以对多个不同的父项选择Child ListBox项,但是在滚动大的列表集时将丢失选择,并且无法访问 7. lstCategory可以直接访问,但是lstSubCategory不能直接访问(可能,我只是不知道如何) 8.我绑定到具有一个复杂对象的ViewModel,该对象将两个ListBoxes表示为两个List对象.

Current Functionality: 1. List item 2. List item 3. Loading of Parent and Child ListBox data is working 4. Multi-select of Parent ListBox items works prefectly 5. Multi-select of Child ListBox items works, but is not accessible 6. Multi-select of Child ListBox items for a number of different parents works in the UI, but the selection is lost when scrolling large list sets and is not accessible 7. lstCategory is accessible directly, but lstSubCategory is not accessible directly (possibly, I just don't know how) 8. I'm bound to a ViewModel with one complex object that represents the two ListBoxes as two List objects.

预期功能: 我希望能够同时选择类别"(父项)和子类别"(子项)ListBox项,如下所示; (X)表示已选择:

Expected Functionality: I would like to be able to select both Category (parent) and SubCategory (child) ListBox items as follows; (X) Denotes Selected:

  • 面包(X)
    • 面包(X)
    • 牛角面包
    • Buscuit(X)
    • 甜甜圈
    • Bread (X)
      • Loaf (X)
      • Croissant
      • Buscuit (X)
      • Donut
      • 小品(X)
      • 草莓
      • 牛奶(X)
      • 果汁(X)
      • 苏打
      • 芯片
      • 薯条
      • 足迹混合

      即使列表很长,我也希望保留选择.因此,我要捕获并使用的是:

      I would like to retain the selections even if this was a long list. So, what I want to capture and work with is:

      • 面包(X)
      • 面包(X)
      • Buscuit(X)
      • 小品(X)
      • 饮料(X)
      • 牛奶(X)
      • 果汁(X)
      • 小吃(X)

      由于每个项目的对象中都有一个CategoryID,因此可以在捕获时剥离层次结构信息.

      Since I have a CategoryID in each of the items' objects, I can strip the heirarchy information on capture.

      为简便起见,这是代码的本质:

      For breivity, here is the essence of the code:

              <ListBox 
                  x:Name="lstCategory"
                  SelectionMode="Multiple" 
                  ItemsSource="{Binding Categories}" 
                  FontSize="32" 
                  Margin="0,0,0,67">
                  <ListBox.ItemTemplate>
                      <DataTemplate>
                          <StackPanel>
                              <StackPanel Orientation="Vertical">
                                  <TextBlock Text="{Binding CategoryName}"
                                              FontSize="36"
                                              TextWrapping="Wrap"
                                              Margin="20,0,0,0"
                                              VerticalAlignment="Top"/>
                                  <StackPanel  Orientation="Vertical" Margin="60,0,0,0">
                                      <ListBox
                                          x:Name="lstSubCategory"
                                          SelectionMode="Multiple" 
                                          ItemsSource="{Binding SubCategories}">
                                          <ListBox.ItemTemplate>
                                              <DataTemplate>
                                                  <TextBlock Text="{Binding SubCategoryName}"
                                                             FontSize="28"
                                                             TextWrapping="Wrap"
                                                             VerticalAlignment="Top"/>
                                              </DataTemplate>
                                          </ListBox.ItemTemplate>
                                      </ListBox>
                                  </StackPanel>
                              </StackPanel>
                          </StackPanel>
                      </DataTemplate>
                  </ListBox.ItemTemplate>
              </ListBox>
      

      和ViewModel:

      And the ViewModel:

          public List<Category> Categories { get; set; }
      
          public PostCategorySelectVM()
          {
              Categories = new List<Category>()
              {
                  new Category() 
                  { 
                      CategoryID = 0, 
                      CategoryName = "Bread",
                      SubCategories = new List<SubCategory>()
                      {
                          new SubCategory() {
                              CategoryID = 001,
                              SubCategoryName = "Loaf"
                          },
                          new SubCategory() {
                              CategoryID = 002,
                              SubCategoryName = "Croissant"
                          }
                          // ...
                      }
                      // ...
                  }
                  // ...
              }
          }
      

      类别:

      public class Category
      {
          public int CategoryID { get; set; }
          public string CategoryName { get; set; }
          public List<SubCategory> SubCategories { get; set; }
      }
      

      SubCategory类:

      SubCategory Class:

      public class SubCategory
      {
          public int CategoryID { get; set; }
          public string SubCategoryName { get; set; }
      }
      

      保存按钮单击事件:

          private void btnSave_Click(object sender, RoutedEventArgs e)
          {
              foreach (Category item in lstCategory.SelectedItems)
              {
                  catList.Add(item);
              }
      
              foreach (Category cat in catList)
              {
                  scatList = cat.SubCategories;
                  foreach (SubCategory scat in scatList)
                  {
                      // How do I select the "Selected" SubCategories?
                      // How do I select the lstSubCategory control?
                  }
              }
          }
      

      最后的注意事项:

      • 我拥有的唯一线索与依赖属性有关,但是我看到的仅有的示例需要FrameworkPresentation.dll,而WP7上没有此框架.
      • 嵌套的ListBox具有预期的UI功能(大列表除去滚动时的交叉选择)
      • 当类别"和子类别"都显示在同一屏幕上时,用户体验会最佳.
      • 考虑UI功能,例如目录搜索引擎.您可能想要选择不同组合的常规类别和/或子类别,但是父级不需要孩子,子级不需要父母,但子级和父级都可以存在(出于特殊性).

      推荐答案

      您可以在数据模板中使用复选框而不是文本框,然后将复选框的IsChecked属性绑定到Category/Subcategory类中的IsSelected属性:

      You could use checkboxes instead of textboxes in your data templates, and then bind the IsChecked property of the checkboxes to an IsSelected property in your Category/Subcategory classes:

          <ListBox x:Name="lstCategory"
              ItemsSource="{Binding Categories}" 
              FontSize="32" 
              Margin="0,0,0,67">
              <ListBox.ItemTemplate>
                  <DataTemplate>
                      <StackPanel Orientation="Vertical">
                          <CheckBox Content="{Binding CategoryName}"
                                      FontSize="36"
                                      IsChecked="{Binding IsSelected,Mode=TwoWay}"
                                      Margin="20,0,0,0"
                                      VerticalAlignment="Top"/>
                          <ListBox ItemsSource="{Binding SubCategories}" Margin="60,0,0,0">
                              <ListBox.ItemTemplate>
                                  <DataTemplate>
                                      <CheckBox Content="{Binding SubCategoryName}"
                                                  FontSize="28"
                                                  IsChecked="{Binding IsSelected,Mode=TwoWay}"
                                                  VerticalAlignment="Top"/>
                                  </DataTemplate>
                              </ListBox.ItemTemplate>
                          </ListBox>
                      </StackPanel>
                  </DataTemplate>
              </ListBox.ItemTemplate>
          </ListBox>
      

      还应该让您的类别/子类别类实现INotifyPropertyChanged,以在设置IsSelected时正确触发通知.

      You should also have your category/subcategory classes implement INotifyPropertyChanged to properly fire notifications when IsSelected is set.

      假设您的保存内容类似于(并非完全正确!)

      assuming that, your save would look something like (this isnt' exact!)

      private void btnSave_Click(object sender, RoutedEventArgs e)
      {
          catList.Clear();
          catList.AddRange( lstCategory.Items.OfType<Category>().Where(x=>x.IsSelected));
      
          scatList.Clear();
          foreach (Category cat in catList)
          {
              scatList.AddRange(cat.SubCategories.Where(x=>x.IsSelected));
          }
      }
      

      这篇关于Cross-ListBox在嵌套的ListBox WP7应用程序中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆