3

I have this ComboBox

<ComboBox Name="company" Width="120" 
                  HorizontalAlignment="Right" Margin="5" 
                  IsSynchronizedWithCurrentItem="True" 
                  ItemsPanel="{DynamicResource Virtualized}" 
                  ItemsSource="{x:Static local:Repository.Customers}" 
                  SelectedItem="{Binding Path=SelectedCustomer}" 
                  DisplayMemberPath="CM_FULL_NAME""/>

It runs. It works. Except in the designer, which won't let me do anything because of the error:

ArgumentException was thrown on "StaticExtention": Exception has been thrown by the target of an invocation.

Detail

The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.

I have tried several things in the static class to skip the constructor in designtime, none of which fix the error:

if (LicenseManager.UsageMode == LicenseUsageMode.DesignTime)
if (DesignerProperties.GetIsInDesignMode(this))
if (System.Reflection.Assembly.GetExecutingAssembly().Location.Contains("VisualStudio"))

And returning in the constructor if any of these are true. Still getting the error.

Edit: Not sure if it makes any difference, but the static repository class uses EF4 to get from a database.

Edit2: Also tried ItemsSource {Binding} to the static lists, still get the same error. Note, calling it a repository is a misnomer, the lists are loaded on startup and never changed. Below answer does not work, still trying to figure this out.

Edit3: Thomas' Suggestion to debug design mode wasn't doable. I am using VS2010 Express, and the tools menu does not have an attach to process option. I still don't know why this breaks the designer and works in runtime.

Kyeotic
  • 19,697
  • 10
  • 71
  • 128

2 Answers2

3

In the getter of the Customers property, try to add this code:

if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
    return null;
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • You can try to debug in design mode, as explained [here](http://stackoverflow.com/questions/2030651/wpf-converter-casting-causes-visual-studio-designer-exception/2030861#2030861) – Thomas Levesque Aug 08 '11 at 21:42
  • Anyway, can't you set the ItemsSource through a binding? I would be cleaner... a static repository is a bad code smell. – Thomas Levesque Aug 08 '11 at 21:43
  • I'm not sure what you mean by that, I was using a static class based on another stackoverflow answer. However, ItemsSource="{Binding Source={x:Static local:Repository.Customers}}" still results in the same error. – Kyeotic Aug 08 '11 at 21:45
  • What I meant is, your ItemsSource should come from a binding on the DataContext, not from a static class. – Thomas Levesque Aug 08 '11 at 21:47
  • Why? The Items are retrieved at startup, reused dozens of times, and never change. – Kyeotic Aug 08 '11 at 21:48
  • A static respository is usually considered bad practice, because the db connection remains open for the whole lifetime of the application. It's recommended to close the connection as soon as possible. – Thomas Levesque Aug 08 '11 at 21:52
  • That's frustrating, I was using a non-static class originally until told to use a static by another stackoverflow user. Does the connection remain open even if the EF entity is from a using block? – Kyeotic Aug 08 '11 at 21:57
  • Could you point me to a good example of how to get a set of tables from a db with EF that will need heavy reuse by an application using the MVVM pattern? I am still learning, and am not sure of the best way to handle these things yet. – Kyeotic Aug 08 '11 at 21:59
  • I don't have an example at hand, but basically you need to load the data from the table in your ViewModel, and bind the ComboBox to the ViewModel – Thomas Levesque Aug 08 '11 at 22:20
  • Yeah, I got that. Please see this question for the problems I ran into when I tried. http://stackoverflow.com/questions/6985283/reusing-binding-collections-for-wpf. The only way to avoid errors is to manually copy the collection each time, which results in duplicated code. I do not consider this a better solution. – Kyeotic Aug 08 '11 at 22:25
2

Thomas answer:

if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
return null;

Works in the static constructor.

Kyeotic
  • 19,697
  • 10
  • 71
  • 128