WPF中C#实现DataGrid数据显示为空问题排查请求
WPF DataGrid绑定模拟数据为空的问题排查与解决
问题描述
尝试在WPF项目的Shortlist.xaml窗口的DataGrid中展示模拟数据,但运行应用程序后DataGrid为空。
问题根源
1. DataGrid未绑定实际数据源
你的XAML中DataGrid仅设置了设计时预览用的d:ItemsSource="{d:SampleData ItemCount=5}",没有绑定到代码中定义的clients集合,导致运行时没有数据来源。
2. DataContext绑定逻辑错误
你将窗口的DataContext直接设置为clients集合本身,而不是窗口实例。这样后续绑定clients属性时,路径会失效——因为此时DataContext已经是集合,无法再找到名为clients的属性。
3. ShortlistedClient类存在冗余字段
ShortlistedClient类中定义了一个多余的clients集合字段,这不仅造成命名混淆,还完全没有必要(实体类不需要包含自身的集合)。
修正方案
步骤1:修改DataGrid的绑定配置
更新Shortlist.xaml中的DataGrid,添加实际运行时的ItemsSource绑定:
<Window x:Class="WpfApp_Employment_Help.Shortlist" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp_Employment_Help" mc:Ignorable="d" Title="Shortlist" Height="450" Width="800"> <Grid> <!-- 添加ItemsSource绑定到窗口的clients属性 --> <DataGrid x:Name="dgr" ItemsSource="{Binding clients}" d:ItemsSource="{d:SampleData ItemCount=5}" Margin="300,95,110,179" SelectionChanged="dgr_SelectionChanged"/> </Grid> </Window>
步骤2:修正DataContext的设置
在Shortlist.xaml.cs中,将DataContext设置为窗口实例本身,确保绑定能找到clients属性:
namespace WpfApp_Employment_Help { public partial class Shortlist : Window { public ObservableCollection<ShortlistedClient> clients { get; set; } = new ObservableCollection<ShortlistedClient>(); public Shortlist() { InitializeComponent(); // 将DataContext设置为窗口自身,而非clients集合 DataContext = this; clients.Add(new ShortlistedClient("Rich", "07111118265", "rich@gmail.com", "Glasgow", "Office", "MSc", "more than 3 years", "Yes", "No")); clients.Add(new ShortlistedClient("Steve", "07567718265", "steve@gmail.com", "Glasgow", "Construction", "High School", "more than 3 years", "Yes", "No")); clients.Add(new ShortlistedClient("Maria", "07485999005", "mb@gmail.com", "Edinburgh", "Office", "MSc", "more than 3 years", "No", "No")); } private void lstShort_SelectionChanged(object sender, SelectionChangedEventArgs e) { } private void dgr_SelectionChanged(object sender, SelectionChangedEventArgs e) { } } }
步骤3:删除冗余字段
移除ShortlistedClient类中多余的clients集合字段:
namespace WpfApp_Employment_Help { public class ShortlistedClient : Client { public DateTime DT { get; private set; } public bool InterestedinVac { get; private set; } // 删除此行冗余代码 // public List<ShortlistedClient> clients { get; set; } = new List<ShortlistedClient>(); public ShortlistedClient(string n, string p, string e, string l, string wt, string q, string we, string dl, string cc) : base(n, p, e, l, wt, q, we, dl, cc) { DT = new DateTime(); InterestedinVac = false; } } }
可选优化(非必须)
Client类中Location、Worktype等属性仅有getter,WPF可以正常显示只读属性,但如果后续需要支持编辑,可给这些属性添加setter:
public string Location { get; set; } public string Worktype { get; set; } public string Qualification { get; set; } public string Workexp { get; set; } public string Drlicence { get; set; } public string Crconviction { get; set; }
内容的提问来源于stack exchange,提问作者Bluetail
相关产品推荐
相关产品推荐

