嵌套的observablecollection列表,如何保持秩序?

时间:2016-05-16 16:14:45

标签: c# list observablecollection

我在一个observablecollection(VideoFileName)中有几个observablecollection。它们包含具有两个属性FileName(字符串)和FileIsSelected(bool)的相同类型的对象。

我希望所有这些内容只在一个列表中SelectedVideoNames),但只有在FileNameshould为真时才必须将FileIsSelected添加到列表中。

这是我的代码:

List<string> DefaultSelectedVideoNames = new List<string> { "empty" };
List<string> SelectedVideoNames = new List<string> { };

for (var j = 0; j < ChannelsCount; j++)
{
    if (uiElement.VideoFileNames[j].Count == 0)
        {
            MessageOutput.AddFrom("SelectedVideoNames", DefaultSelectedVideoNames);
        }
        else
        {
            for (var k = 0; k < uiElement.VideoFileNames[j].Count; k++)
            {
                if (uiElement.VideoFileNames[j][k].FileIsSelected == true)
                {
                    SelectedVideoNames.Add(uiElement.VideoFileNames[j][k].FileName);
                }
            }
        }
    }
}

问题是当FileIsSelected值更改时,它会被添加到列表的末尾,但我想保留原始observablecollection的顺序。

我该怎么做?

一个小模式可以更好地解释我的问题:

(observablecollection) Video Selector 0
    --->Video (FileName = "John", FileIsSelected = "true")
    --->Video (FileName = "Dany", FileIsSelected = "false")
    --->Video (FileName = "Bob", FileIsSelected = "true")
    ...

(observablecollection) Video Selector 1
    --->Video (FileName = "Henry", FileIsSelected = "false")
    --->Video (FileName = "Jack", FileIsSelected = "false")
    --->Video (FileName = "Chris", FileIsSelected = "false")
    ...

(observablecollection) Video Selector 2
    --->Video (FileName = "Jimmy", FileIsSelected = "true")
    --->Video (FileName = "Tim", FileIsSelected = "true")
    --->Video (FileName = "Donald", FileIsSelected = "true")
    ...

最终名单应为

John, Bob, Jimmy, Tim, Donald

在运行时,例如,名为“Dany”的视频将FileIsSelected设置为true,因此列表变为:

John, Dany, Bob, Jimmy, Tim, Donald

但现在我得到这样的东西:

John, Bob, Jimmy, Tim, Donald, Dany

0 个答案:

没有答案