数据绑定导致 WPF 设计器错误
有谁知道为什么此绑定会导致 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢打赌 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.
尝试用“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.