WPF 数据网格单元格为空

发布于 2024-11-03 16:24:34 字数 1917 浏览 4 评论 0原文

我在 WPF 中使用 DataGrid 控件时遇到了一个非常奇怪的问题。当我在代码中设置 DataGrid 的 ItemsSource 后,会生成行,但单元格完全是空的!在使用 DataGrids 之前我从未遇到过这个问题,并且我知道绑定正在工作,因为我有另一个应用程序可以执行类似的操作并且它可以在那里工作。我还尝试让它自动为我生成列,但它仍然不起作用,事实上甚至没有生成列。下面是 XAML 中的 DataGrid、填充 DataGrid 的代码以及我的模型。

注意:FindMoviesByName 返回 MovieInfo[]

<DataGrid x:Name="dgdMovies" AutoGenerateColumns="False" MaxHeight="450" 
                        ScrollViewer.CanContentScroll="True" 
                        ScrollViewer.VerticalScrollBarVisibility="Auto" 
                        ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Movie Title" Width="150"  Binding="{Binding Title}"></DataGridTextColumn>
        <DataGridTextColumn Header="IMDB Location" Width="150"  Binding="{Binding ImdbUrl}"></DataGridTextColumn>
        <DataGridTextColumn Header="Genre" Width="150"  Binding="{Binding Genre}"></DataGridTextColumn>
        <DataGridTextColumn Header="MPAA Rating" Width="150"  Binding="{Binding MpaaRating}"></DataGridTextColumn>
        <DataGridTextColumn Header="Personal Rating" Width="150"  Binding="{Binding PersonalRating}"></DataGridTextColumn>
        <DataGridTextColumn Header="Critics Rating" Width="150"  Binding="{Binding CriticsRating}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>
var results = _updateService.FindMoviesByName(txtSearch.Text);
        lblResults.Content = results.Length + " Results!";
        dgdMovies.ItemsSource = results;

[DataContract]
public class MovieInfo
{
    [DataMember] public string Title;
    [DataMember] public string ImdbUrl;
    [DataMember] public Genre Genre;
    [DataMember] public MpaaRating MpaaRating;
    [DataMember] public int CriticsRating;
    [DataMember] public int PersonalRating;
}

I'm having a really weird problem using the DataGrid control in WPF. After I set the ItemsSource of the DataGrid in code, the rows get generated but the cells are completely empty! I've never had this problem before using DataGrids, and I know that the bindings are working because I have another application that does something similar and it works on there. Also I tried having it auto generate my columns for me and it still doesn't work, in fact no columns are even generated. Below is my DataGrid in XAML, my code to populate the DataGrid, and my model.

Note: FindMoviesByName returns MovieInfo[]

<DataGrid x:Name="dgdMovies" AutoGenerateColumns="False" MaxHeight="450" 
                        ScrollViewer.CanContentScroll="True" 
                        ScrollViewer.VerticalScrollBarVisibility="Auto" 
                        ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Movie Title" Width="150"  Binding="{Binding Title}"></DataGridTextColumn>
        <DataGridTextColumn Header="IMDB Location" Width="150"  Binding="{Binding ImdbUrl}"></DataGridTextColumn>
        <DataGridTextColumn Header="Genre" Width="150"  Binding="{Binding Genre}"></DataGridTextColumn>
        <DataGridTextColumn Header="MPAA Rating" Width="150"  Binding="{Binding MpaaRating}"></DataGridTextColumn>
        <DataGridTextColumn Header="Personal Rating" Width="150"  Binding="{Binding PersonalRating}"></DataGridTextColumn>
        <DataGridTextColumn Header="Critics Rating" Width="150"  Binding="{Binding CriticsRating}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>
var results = _updateService.FindMoviesByName(txtSearch.Text);
        lblResults.Content = results.Length + " Results!";
        dgdMovies.ItemsSource = results;

[DataContract]
public class MovieInfo
{
    [DataMember] public string Title;
    [DataMember] public string ImdbUrl;
    [DataMember] public Genre Genre;
    [DataMember] public MpaaRating MpaaRating;
    [DataMember] public int CriticsRating;
    [DataMember] public int PersonalRating;
}

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

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

发布评论

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

评论(1

灰色世界里的红玫瑰 2024-11-10 16:24:34

这些是字段,而不是属性。数据绑定仅适用于属性。

Those are fields, not properties. Databinding only works with properties.

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