开发者

Can WPF handle Guids from LINQ to SQL?

开发者 https://www.devze.com 2023-01-25 14:43 出处:网络
I stumbled on a strange problem. In my WPF (.NET 4) window I have a simple combobox (DisplayMemberPath=\"Name\" SelectedValuePath=\"开发者_如何学GoId\"). When I load the window I set the combobox\'s I

I stumbled on a strange problem. In my WPF (.NET 4) window I have a simple combobox (DisplayMemberPath="Name" SelectedValuePath="开发者_如何学GoId"). When I load the window I set the combobox's ItemSource property to context.Currencies.ToList() using LINQ to SQL. The Currency table in SQL is simply [Id] [uniqueidentifier] NOT NULL PRIMARY KEY, [Name] char NOT NULL. In .NET this translates to Id = System.Guid, Name = System.String.

The problem I'm having is that the call to combobox.ItemsSource = context.Currencies.ToList(); throws a FormatException (Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)).

What I can't understand is why is this error thrown?

If I leave LINQ to SQL out of the picture, define a "test" class - public class Test { public Guid Id; public string Name; }, set the comobox.ItemsSource to a List<Test> which contains some entries, then everything works.

If I do this:

combobox.ItemsSource = context.Currencies.Select(c => new { c.Id.ToString(), c.Name }).ToList()

then everything works.

I tried setting the current thread's Culture and UICulture to "en-US" beforehand as well (just in case it's a culture problem, my default culture is "et-EE"), but that didn't help either.

I looked at the generated LINQ to SQL classes from the designer, but couldn't find any properties to alter which might have helped with this error.

Am I missing something here or is this really a bug in the .NET framework?´

PS! The Currency table contains valid entries (valid Guids).


As Thomas pointed out, I had a binding on SelectedValue and it turns out the binding also contained a FallbackValue = 1. After removing the fallback property everything works. The property was scrolled out of view, so indeed it was something I missed.

0

精彩评论

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