数据绑定导致 WPF 设计器错误

发布于 2024-08-19 14:52:56 字数 1171 浏览 3 评论 0原文

有谁知道为什么此绑定会导致 WPF 设计器中出现错误? (“调用的目标已引发异常。”)

XAML(部分):

<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
        }
    }
}

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
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2024-08-26 14:52:56

我敢打赌 PopulateClientList() 内有一些代码在从设计器运行代码的 AppDomain 运行时会失败。

如果您从文件加载 XML,则物理路径可能不在相同的相对位置或类似位置。

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.

万劫不复 2024-08-26 14:52:56

尝试用“clr-namespace:MyAppNamespace; assembly =”替换包含语句添加程序集。您不必输入程序集名称。这是 Visual Studio 的一个错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文