wpf组合框异步绑定到项目源问题

问题描述:

我有一个mvvm应用程序,主窗口是一个选项卡控件。 我使用itemssource绑定项目组合框, 一切工作正常,直到我去另一个选项卡,由于某种原因组合框中选定的项目获得空值,任何想法?wpf组合框异步绑定到项目源问题

的结合是onpropertychanged双向updatesource和财产的的ObservableCollection

+0

你能发布有关XAML和/或C#?这可以帮助我们弄清楚你想要做什么并缩小问题的范围。 – Andy 2009-07-11 04:30:18

我以前有同样的问题。解决方案是确保XAML中的组合框的Itemsource属性尚未在SelectedValue属性之前进行声明。它应该工作。

型我有几乎一模一样的场景的MVVM应用程序。主窗口有一个选项卡控件。有一个包含组合框的选项卡。组合框项目源绑定到IList(在视图模型中),并且选定值绑定到实现INotifyPropertyChanged的视图模型中的属性。

<ComboBox ItemsSource="{Binding AllowedJudges}" 
        SelectedValue="{Binding SelectedJudge, UpdateSourceTrigger=PropertyChanged}" > 

当选择另一个选项卡时,绑定到SelectedValue的视图模型的属性被神秘地设置为null。我能够通过不允许的SelectedValue绑定属性来处理它被设置为null:

public Judge SelectedJudge 
    { 
    get { return selectedJudge; } 
    set 
    { 
     if(selectedJudge==value || value==null) return; 
     selectedJudge = value; 
     OnPropertyChanged("SelectedJudge"); 
     updateViewData(); 
    } 
    } 

然而,这是我不明白为什么一个选项卡窗格成为无形意味着在组合框中有变为取消的值...

+0

这是我的问题两个 – 2009-08-15 14:04:08

+0

是否有任何理由为什么你使用SelectedValue而不是SelectedItem? 只要你知道你在做什么,不一定是一个错误,只是激怒我。 查看updateViewData中发生的操作可能有助于解决您的问题。 – 2009-09-03 11:53:27

如果由于某种原因,ItemsSource的BindingSource不再包含SeletedItem(因为它已重新初始化或其他),那么SelectedItem可以重置为默认值,即null。 从你的例子我不知道为什么会发生这种情况,但也许是因为你错过了添加更多的环境代码。

这可能是你的viewmodel ierarchy的问题。 例如,如果Binding的两端都是依赖项属性,并且ownertype属性未绑定到特定的类(例如,设置父类),则此依赖项属性将由所有继承者一起使用。糟糕的设计

这只是必要对的ObservableCollection工作,为的AddRange只加载(不使用“添加”)

当我们加载数据:然后将第一后

foreach (object item in items) 
{ 
    MyList.Add(item); // Where MyList is a ObservableCollection 
} 

ObservableCollection调用OnCollectionChanged。 然后,ComboBox将尝试选择SelectedValue,并在找不到此元素时返回'null'。

它可以解决一个问题:

using System.Collections.Generic; 
using System.Collections.Specialized; 
using System.ComponentModel; 

namespace System.Collections.ObjectModel 
{ 
    /// <summary> 
    /// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. 
    /// </summary> 
    /// <typeparam name="T"></typeparam> 
    public class ObservableCollectionEx<T> : ObservableCollection<T> 
    { 
     /// <summary> 
     /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class. 
     /// </summary> 
     public ObservableCollectionEx() 
      : base() { } 

     /// <summary> 
     /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection. 
     /// </summary> 
     /// <param name="collection">collection: The collection from which the elements are copied.</param> 
     /// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception> 
     public ObservableCollectionEx(IEnumerable<T> collection) 
      : base(collection) { } 

     /// <summary> 
     /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). 
     /// </summary> 
     public void AddRange(IEnumerable<T> collection) 
     { 
      // 
      // Add the items directly to the inner collection 
      // 
      foreach (var data in collection) 
      { 
       this.Items.Add(data); 
      } 

      // 
      // Now raise the changed events 
      // 
      this.OnPropertyChanged(new PropertyChangedEventArgs("Count")); 
      this.OnPropertyChanged(new PropertyChangedEventArgs("Item[]")); 

      this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); 
     } 

    } 
}