0

我想在洗牌的ObservableCollection內容,洗牌而初始化觀察集合

public static ObservableCollection<whatsnewCompleteData> whatsnewCompleteList = new ObservableCollection<whatsnewCompleteData>(); 

for (int i = 0; i < whatsnewfeed.feed.entry.Count; i++) 
{    
    whatsnewCompleteList.Add(new whatsnewCompleteData(i, content[i]); 
} 

如果是正常的名單,我可以很容易地進行洗牌!但observablecollection直接綁定到UI。我唯一能做的就是每次初始化時使用一個隨機數。 所以這裏的範圍內,

0 < whatsnewfeed.feed.entry.Count 

如何生成一個隨機數,並覆蓋從0所有值whatsnewfeed.feed.entry.count?

回答

0

首先,你可以洗牌在就地ObservableCollection的數據,你的建議,或者你可以創建一個新的,然後覆蓋ObservableCollection

考慮這樣的情況:

public static ObservableCollection<whatsnewCompleteData> whatsnewCompleteList = new ObservableCollection<whatsnewCompleteData>(); 

// A bindable property 
public ObservableCollection<whatsnewCompleteData> WhatsNewCompleteList 
{ 
    get 
    { 
     return whatsnewCompleteList; 
    } 
    set 
    { 
     whatsnewCompleteList = value; 
     // Your property changed notifier method 
     OnPropertyChanged("WhatsNewCompleteList"); 
    } 
} 

在你的方法:

// content should obviously be whatever object type you need to get 
// (or already have) 
public void Shuffle(List<object> content) 
{ 
    // Note: you don't have to initialize an observable collection like this. 
    // You can also create a list, shuffle it as you would normally, then call 
    // new ObservableCollection<whatsnewCompleteData(shuffledList); 
    var newWhatsnewCompleteList = new ObservableCollection<whatsnewCompleteData>(); 

    for (int i = 0; i < whatsnewfeed.feed.entry.Count; i++) 
    {    
     newWhatsnewCompleteList.Add(new whatsnewCompleteData(i, content[i]); 
    } 
    WhatsNewCompleteList = newWhatsnewCompleteList; 
} 

這將覆蓋當前目錄,也不必擔心UI嚇壞了更新綁定。

另一種選擇是簡單地創建一個新的值列表,然後在static集合上調用Clear()並將其填入新值。

希望這有助於和快樂的編碼!

+0

知道了Nate,這從來沒有打動我的腦海!謝謝! –