开发者

Problem using Linq to Sql queries DataBinding to a WPF Combobox

开发者 https://www.devze.com 2023-03-29 22:12 出处:网络
I have the following WPF markup <ComboBox x:Name=\"realmComboBox\" DisplayMemberPath=\"Name\" SelectedValuePath=\"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 i开发者_如何学Cn _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?


To elaborate on my comment, this is a public field:

public string Name;

And this is a public property:

public string Name { get; set; }

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消