WPF 数据网格单元格为空
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些是字段,而不是属性。数据绑定仅适用于属性。
Those are fields, not properties. Databinding only works with properties.