【问题标题】:Clearing selecteditem of listbox (which is bound to collection of objects) with MVVM使用 MVVM 清除列表框的选定项(绑定到对象集合)
【发布时间】:2011-01-11 01:40:36
【问题描述】:

我有一个带有 silverlight 4 的列表框。我将列表绑定到一个对象列表。

1.) SelectedValue 属性绑定到名为 Current 的视图模型的公共属性。如何清除选择?我尝试将 Current 的值设置为 null。好吧,这清除了选择,但是它也破坏了编辑表单中的绑定,该表单有一个绑定到“当前”属性的组合框。绑定到 Current.FirstName 等的文本框工作正常,但是在我将 Current 对象设置为 null 后组合框不起作用。

2.) 如何在不选择第一项的情况下加载表单?

【问题讨论】:

  • 我认为这是一个过时的问题。唯一需要的是将“当前”设置为空。如果绑定很好,这应该可以工作。见silverlightshow.net/items/…

标签: silverlight binding


【解决方案1】:

在 Silverlight 中找到了解决此错误的方法:

// Bug in SL listbox prevents SelectedIndex = -1 from unselected.
// Workaround is to use DispatcherBeginInvoke to do it async. Found
// work around here:
// http://sharplogic.com/blogs/rdavis/PermaLink,guid,2f5bbfa1-4878-490f-967d-bf00bc04dfde.aspx
Dispatcher.BeginInvoke(() => { QuickItemsListBox.SelectedIndex = -1; });

更多细节在这里: http://sharplogic.com/blogs/rdavis/PermaLink,guid,2f5bbfa1-4878-490f-967d-bf00bc04dfde.aspx

【讨论】:

    【解决方案2】:

    不确定何时修复,但 VoodooChild 的答案现在在 Silverlight 5 中有效。将其传递给其他人以防万一。

    yourCB.selectedIndex = -1;
    

    【讨论】:

      【解决方案3】:

      试试:

      yourCB.SelectedIndex = -1;

      【讨论】:

      • @cek:嗯,我很抱歉 - 不知道这是一个错误。
      猜你喜欢
      • 2015-03-18
      • 2013-01-17
      • 2011-03-23
      • 2015-09-10
      • 2011-04-15
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2014-11-05
      相关资源
      最近更新 更多