WPF ListView 绑定 +错误

发布于 2024-11-03 02:12:14 字数 1200 浏览 0 评论 0原文

我收到异常“未将对象引用设置为对象的实例”。在“songs.DataContext =”行上。如果我添加歌曲= new ListView();在此之前,即使音频文件列表不是

XAML,我的列表视图也是空的:

<ListView  Height="Auto" HorizontalAlignment="Center" ItemsSource="{Binding}"
               VerticalAlignment="Center" Name="songList" Width="Auto" MinHeight="300" MinWidth="600">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="Auto" Header="Title" DisplayMemberBinding="{Binding Path=Title}" />
                <GridViewColumn Width="Auto" Header="Artist" DisplayMemberBinding="{Binding Path=Artist}" />
                <GridViewColumn Width="Auto" Header="Album" />
                <GridViewColumn Width="Auto" Header="Length" />
            </GridView>
        </ListView.View>
    </ListView>

C#

 public struct AudioFile
    {

        public String Artist;
        public String Title;
        public String Album;
        public String fileLocation;
        public String Length;
    }

//...
private List<AudioFile> songs = new List<AudioFile>();
//code that adds to array
songList.DataContext = songs;

I am getting an exception "Object reference not set to an instance of an object." on the "songs.DataContext =" line. If I add songs = new ListView(); before it my listview is empty even though the list of audiofiles is not

XAML:

<ListView  Height="Auto" HorizontalAlignment="Center" ItemsSource="{Binding}"
               VerticalAlignment="Center" Name="songList" Width="Auto" MinHeight="300" MinWidth="600">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="Auto" Header="Title" DisplayMemberBinding="{Binding Path=Title}" />
                <GridViewColumn Width="Auto" Header="Artist" DisplayMemberBinding="{Binding Path=Artist}" />
                <GridViewColumn Width="Auto" Header="Album" />
                <GridViewColumn Width="Auto" Header="Length" />
            </GridView>
        </ListView.View>
    </ListView>

C#

 public struct AudioFile
    {

        public String Artist;
        public String Title;
        public String Album;
        public String fileLocation;
        public String Length;
    }

//...
private List<AudioFile> songs = new List<AudioFile>();
//code that adds to array
songList.DataContext = songs;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月竹挽风 2024-11-10 02:12:14

我怀疑您的代码位于构造函数中,位于尚未创建songList 的位置。

//...
private List<AudioFile> songs = new List<AudioFile>();
//code that adds to array
songList.DataContext = songs;

尝试在 Loaded 事件中移动它。

I suspect your code to be in the constructor, in a place where songList is not yet created.

//...
private List<AudioFile> songs = new List<AudioFile>();
//code that adds to array
songList.DataContext = songs;

Try to move it in the Loaded event instead.

微暖i 2024-11-10 02:12:14

您的歌曲 显然已经完成,但是songList 又如何呢?

Your songs are clearly instatiated, but what about the songList?

弥繁 2024-11-10 02:12:14

我认为您可能正在尝试在构造函数中设置 SongList.ItemsSource = list ,显然该对象尚未构建。

在您的 UI 类中,初始化的最佳位置仅在 Loaded 事件中或在 InitializeComponent 方法之后。

更好的方法是使用 MVVM。

I think you may be trying to set songList.ItemsSource = list in your constructor and obviously the object is not built yet.

In your UI classes, best place to initialize will be only in Loaded event or after InitializeComponent method.

Even better approach will be to use MVVM.

梦过后 2024-11-10 02:12:14

编写此代码

//...
private List<AudioFile> songs = new List<AudioFile>();
//code that adds to array
songList.DataContext = songs;

您是否在为视图调用 IniitializeComponent() 方法之前 ?您能否提供有关代码放置的更多见解,这将有助于更好地理解情况。

只是一个建议,但不相关。我会说使用类而不是结构对象,因为 WPF 数据绑定仅考虑属性,而不考虑字段。 当然,这不是错误的原因。

Are you writing this code -

//...
private List<AudioFile> songs = new List<AudioFile>();
//code that adds to array
songList.DataContext = songs;

before IniitializeComponent() method is called for a view?? Can you provide a bit more of insight regarding your code placement that will help in understanding the situation better.

And just a suggestion, not related though. I would say use class instead of structure objects because WPF data binding considers only properties, not fields. Definitely, this is not a cause of an error though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文