[英]wpf ComboBox Async binding to items source problem
我有一個mvvm應用程序,主窗口是一個選項卡控件。 我使用itemssource將項目綁定到組合框,一切正常,直到我轉到另一個選項卡,由於某種原因組合框的選定項目獲得空值,任何想法?
綁定是twoway updatesource onpropertychanged,屬性是observablecollection的類型
我之前遇到過同樣的問題。 解決方案是確保在SelectedValue屬性之前未聲明XAML中comboBox的Itemsource屬性。 它應該工作。
只需要處理一個ObservableCollection並加載為AddRange(不要使用'Add')
當我們加載數據時:
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));
}
}
}
我有一個mvvm應用程序幾乎完全相同的情況。 主窗口有一個選項卡控件。 有一個包含組合框的標簽。 組合框項目源綁定到IList(在視圖模型中),並且Selected值綁定到實現INotifyPropertyChanged的視圖模型中的屬性。
<ComboBox ItemsSource="{Binding AllowedJudges}"
SelectedValue="{Binding SelectedJudge, UpdateSourceTrigger=PropertyChanged}" >
選擇另一個選項卡時,視圖模型綁定到SelectedValue的屬性會神秘地設置為null。 我能夠通過不允許將SelectedValue-bound屬性設置為null來處理它:
public Judge SelectedJudge
{
get { return selectedJudge; }
set
{
if(selectedJudge==value || value==null) return;
selectedJudge = value;
OnPropertyChanged("SelectedJudge");
updateViewData();
}
}
但是,我不清楚為什么一個標簽窗格變得不可見意味着組合框中的值會被取消選擇....
如果由於某種原因,ItemsSource的BindingSource不再包含SeletedItem(因為它被重新初始化或其他),那么SelectedItem可以重置為默認值,即null。 從你的例子我不知道為什么會發生這種情況,但這可能是因為你錯過了添加更多的環境代碼。
它可能是你的viewmodel ierarchy的問題。 例如,如果Binding的兩端都是依賴項屬性,並且ownertype屬性未綁定到特定類(例如,設置父類),則所有繼承器將一起使用此依賴項屬性。 糟糕的設計
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.