使用 Linq to Sql 查询数据绑定到 WPF 组合框时出现问题
我有以下 WPF 标记,
<ComboBox x:Name="realmComboBox"
DisplayMemberPath="Name"
SelectedValuePath="Name"
Width="120" />
我在网上找到了许多示例,这些示例表示以下其中一项应该可以工作
realmComboBox.ItemsSource = from realm in _db.Realms select realm;
realmComboBox.ItemsSource = (from realm in _db.Realms select realm).ToList();
,但我得到的只是一个空白下拉列表。如果您不设置 DisplayMemberPath,甚至不会发生我被告知的 ToString 问题。我发现唯一有效的是以下内容
realmComboBox.ItemsSource = from realm in _db.Realms
select new {
Name = realm.Name
};
但这感觉完全是资源浪费,因为我已经在内存中拥有了 Realm 对象,并且它显然有一个 Name 属性。我缺少什么?
I have the following WPF markup
<ComboBox x:Name="realmComboBox"
DisplayMemberPath="Name"
SelectedValuePath="Name"
Width="120" />
I've found numerous examples on the web that say one of the following should work
realmComboBox.ItemsSource = from realm in _db.Realms select realm;
realmComboBox.ItemsSource = (from realm in _db.Realms select realm).ToList();
but all I get is a blank drop down. Not even the ToString problem that I'm told happens if you don't set DisplayMemberPath. The only thing I have found that works is the following
realmComboBox.ItemsSource = from realm in _db.Realms
select new {
Name = realm.Name
};
But this feels like a total waste of resources since I already have the Realm object in memory and it clearly has a Name property. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了详细说明我的评论,这是一个公共字段:
这是公共 属性:
因为它适用于仅使用属性的匿名类型我假设你的数据只有公共字段。
另外:即时窗口不是输出窗口,您可能需要通过
View > 显示它输出
。To elaborate on my comment, this is a public field:
And this is a public property:
Since it works with the anonymous type which only uses properties i would assume your data only has public fields.
Also: the immediate-window is not the output-window, you may need to show it via
View > Output
.