wpf 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.ObjectModel.ObservableCollection”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19265424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:48:58  来源:igfitidea点击:

Cannot implicitly convert type 'System.Collections.Generic.List to 'System.Collections.ObjectModel.ObservableCollection

c#wpfxamllistbox

提问by Badrul

I am trying to return collection and assign it to a listbox but I am getting following error

我正在尝试返回集合并将其分配给列表框,但出现以下错误

"Cannot implicitly convert type 'System.Collections.Generic.List to 'System.Collections.ObjectModel.ObservableCollection"

“无法将类型 'System.Collections.Generic.List 隐式转换为 'System.Collections.ObjectModel.ObservableCollection”

I am new to WPF & C# I got no clue how to handle this.

我是 WPF 和 C# 的新手,我不知道如何处理这个问题。

All I want to do is load all videos in My Videos folder into listbox containing media element control.

我想要做的就是将我的视频文件夹中的所有视频加载到包含媒体元素控件的列表框中。

What would be the correct way to return?

什么是正确的返回方式?

Code:

代码:

public class Video 
{
    public Uri SourceUri { get; set; }

    public static ObservableCollection<Video> LoadVideoInfo()
    {
        List<Video> videoresult = new List<Video>();

            foreach (string filename in
            System.IO.Directory.GetFiles(
            Environment.GetFolderPath(
            Environment.SpecialFolder.MyVideos)))

            videoresult.Add(new Video { SourceUri = new UriBuilder(filename).Uri });

        return videoresult;
    }
}

XAML:

XAML:

<ListBox x:Name="VideoList" ItemsSource="{Binding }" Width="auto" Height=" auto" Margin="5,0,5,2" Grid.ColumnSpan="2" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <MediaElement Source="{Binding SourceUri}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

采纳答案by Sriram Sakthivel

Try this

尝试这个

ObservableCollection<Video> videoresult = new ObservableCollection<Video>();

回答by SynerCoder

Your method says it returns a ObservableCollection<Video>but you return a List<Video>. Create a ObservableCollection<Video>and return it.

你的方法说它返回 aObservableCollection<Video>但你返回 a List<Video>。创建一个ObservableCollection<Video>并返回它。

return new ObservableCollection<Video>(videoresult);

Multieple DataContexts:

多个数据上下文:

ContextModel

上下文模型

public class ContextModel
{
    public ObservableCollection<Video> Videos { get; set; }
    public object OtherContext { get; set; }
}

Main Window

主窗口

this.DataContext = new ContextModel()
{
    Videos = Video.LoadVideoInfo(),
    OtherContext = LoadOtherContext()
};

Main Window Xaml

主窗口 Xaml

<ListBox x:Name="VideoList" ItemsSource="{ Binding Videos }" />
<ListBox x:Name="OtherListBox" ItemsSource="{ Binding OtherContext }" />

回答by Rajesh Subramanian

in return statement, use the following code.

在 return 语句中,使用以下代码。

return (new ObservableCollection(videoresult));

回答by J R B

you should change in return type , return type should be ObservableCollection instead of List.

你应该改变返回类型,返回类型应该是 ObservableCollection 而不是 List。