【问题标题】:ObservableCollection filter Selected ItemObservableCollection 过滤器选择的项目
【发布时间】:2012-09-18 18:09:44
【问题描述】:

我有一个不知道如何解决的问题。

我有一个可观察的集合,当我在文本框中输入时过滤项目,问题是当我选择过滤的项目时,我得到错误的选择索引。

例如,在过滤实际选定的索引为 2 后,我有一个项目,但因为它在我键入时设置集合,如果剩下的唯一过滤项目是 1,它会将索引设置为 1。

那么我如何选择正确的项目。就像在邮件应用程序中一样,让我的问题更容易理解

这里是选择改变事件:

private void searchToDoItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (searchToDoItemsListBox.SelectedIndex == -1)
        return;
    NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItemSearch=" + searchToDoItemsListBox.SelectedIndex, UriKind.Relative));
    searchToDoItemsListBox.SelectedIndex = -1;
}

这里是详细信息页面:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    if (NavigationContext.QueryString.TryGetValue("selectedItemSearch", out selectedIndexSearch))
    {
        int indexSearch = int.Parse(selectedIndexSearch);
        DataContext = App.ViewModel.AllToDoItems[indexSearch];
    }

}

【问题讨论】:

  • whathaveyoutried?贴一些代码
  • 这是我的选择更改事件 private void searchToDoItemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (searchToDoItemsListBox.SelectedIndex == -1) return; NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItemSearch=" + searchToDoItemsListBox.SelectedIndex, UriKind.Relative)); searchToDoItemsListBox.SelectedIndex = -1; } 然后在详细信息页面上我有一个导航到事件
  • 请用您评论中的代码更新您的问题

标签: c# .net observablecollection selectedindex


【解决方案1】:

绑定到 SelectedItem

<ListBox SelectedItem="{Binding Selected, Mode=TwoWay}" ItemsSource="Binding={Items}">
</ListBox>

你必须字段:

public ObservableCollection<ItemType> Items {get;set;} //setted while filtering, does it?

private ItemType _selected;
public ItemType Selected
{
  get 
  {
    return _selected;
  }
  set 
  { 
     _selected = value;
     //here you can save the item. 
     //For example save the item id, and navigate to DetailsPage
  }
}

然后,您可以从列表中获取项目:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        if (NavigationContext.QueryString.TryGetValue("selectedItemSearch", out selectedIndexSearch))
        {
            int id = int.Parse(selectedIndexSearch);
            DataContext = GetById(id)
        }
    }

  public ItemType GetByIf(id) 
  {
    for(int i = 0; i < App.ViewModel.AllToDoItems.Count; i++)
    {
        if(App.ViewModel.AllToDoItems[i].Id == id) return App.ViewModel.AllToDoItems[i];
    }
    return null;
  }

【讨论】:

  • 在此处遇到一些问题,即“长度”扩展不存在,我收到此错误运算符 '
  • 好吧,我想,AllToDoItems 是一个数组。它是什么?是字典吗?
  • 添加了一些代码作为答案,以便更容易看到我搞砸了。
【解决方案2】:

现在已经这样做了,我现在什么也得不到。它可以导航,但什么也没有显示。

if (NavigationContext.QueryString.TryGetValue("selectedItemSearch", out selectedIndexSearch))
        {
            //int indexSearch = int.Parse(selectedIndexSearch);
            //DataContext = App.ViewModel.AllToDoItems[indexSearch];

            int id = int.Parse(selectedIndexSearch);
            DataContext = GetById(id);
        }

public object GetById(int id)
{
    for(int i = 0; i < App.ViewModel.AllToDoItems.Count; i++)
    {
        if (App.ViewModel.AllToDoItems[i].ToDoItemId == id) 
        return App.ViewModel.AllToDoItems[i];
    }
    return null;
}

AllToDoItems 如下所示,它是一个可观察的集合; 这是在下面从数据库加载集合的 ViewModel 中。 ToDoItem 是 Model 中的表名。

        // Specify the query for all to-do items in the database.
        var toDoItemsInDB = from ToDoItem todo in toDoDB.Items
                            select todo;

        // Query the database and load all to-do items.
        AllToDoItems = new ObservableCollection<ToDoItem>(toDoItemsInDB);

模型如下所示:

     public Table<ToDoItem> Items;
    //public Table<ToDoFavCategory> Categories;
}

[Table]
public class ToDoItem : INotifyPropertyChanged, INotifyPropertyChanging
{

    // Define ID: private field, public property, and database column.
    private int _toDoItemId;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int ToDoItemId
    {
        get { return _toDoItemId; }
        set
        {
            if (_toDoItemId != value)
            {
                NotifyPropertyChanging("ToDoItemId");
                _toDoItemId = value;
                NotifyPropertyChanged("ToDoItemId");
            }
        }
    }

查看我构建它的链接可能更容易 http://msdn.microsoft.com/en-us/library/hh286405(v=vs.92).aspx

【讨论】:

  • 将光标放在 DataContext = GetById(id); 行并按 F9。然后运行应用程序(调试模式)。并检查 id 值(悬停它)。对吗?
  • GetById 是选定的索引。嗯
  • GetById 返回 object 但它必须返回正确的类型。它是什么类型的?
  • 不明白你所说的类型是什么意思,如果它是一个 int 或 string?
  • 好的,给我看看 AllToDoItems 的完整声明。它是什么的 ObservableCollection?
猜你喜欢
  • 2014-05-14
  • 2016-10-31
  • 2020-07-21
  • 2011-10-27
  • 2017-09-24
  • 1970-01-01
  • 2021-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多