views:

146

answers:

2

Hey guys

Does anyone know why this binding is causing an error in the WPF designer? ("Exception has been thrown by the target of an invocation.")

XAML (partial):

<Window xmlns:local="clr-namespace:MyAppNamespace">
    <DataGrid ItemsSource="{Binding Source={x:Static local:Clients.Instance},
                                    Path=ClientList}" />
</Window>

C#:

namespace MyAppNamespace
{
    public sealed class Clients
    {
        // Singleton pattern
        public static readonly Clients Instance = new Clients();
        private Clients() { }
        static Clients()
        {
            clientList = new ObservableCollection<Client>();
            PopulateClientList();
        }

        private static ObservableCollection<Client> clientList;
        public static ObservableCollection<Client> ClientList
        {
            get { return clientList; }
            set { clientList = value; }
        }

        public static void PopulateClientList()
        {
            // .. load client data from xml
        }

        public class Client
        {
            // ... expose public properties for fields in provided xml element
        }
    }
}
+2  A: 

I would bet there's some code inside PopulateClientList() that's failing when run from the AppDomain the designer is running the code from.

If you're loading XML from a file, maybe the physical path isn't in the same relative location, or something like that.

Mike Schenk
Per my comment above, he can drill down through InnerException to verify this hunch and determine the original cause.
itowlson
Correct answer! The path to my XML file was flummoxing the designer... System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Matt Jenkins
+1  A: 

try replacing your include statement with "clr-namespace:MyAppNamespace;assembly=" adding the assembly. you dont have to enter the assembly name. its a bug with visual studio.

Steve Psaltis
Thank you for your advice Steve.
Matt Jenkins