将 WPF 数据网格绑定到数据表
我使用了发布在以下位置的精彩示例:
http://www.codeproject.com/KB /WPF/WPFDataGridExamples.aspx
将 WPF 数据网格绑定到数据表。
下面的源代码编译良好;它甚至可以运行并显示 WPF 数据网格中 InfoWork 数据表的内容。万岁!但带有数据网格的 WPF 页面将不会显示在设计器中。我在我的设计页面上收到了一个难以理解的错误,该错误显示在本文末尾。我假设设计者在实例化数据视图以在网格中显示时遇到一些困难。我该如何解决这个问题?
XAML 代码:
xmlns:local="clr-namespace:InfoSeeker"
<Window.Resources>
<ObjectDataProvider
x:Key="InfoWorkData"
ObjectType="{x:Type local:InfoWorkData}" />
<ObjectDataProvider
x:Key="InfoWork"
ObjectInstance="{StaticResource InfoWorkData}"
MethodName="GetInfoWork" />
</Window.Resources>
<my:DataGrid
DataContext="{Binding Source={StaticResource InfoWork}}"
AutoGenerateColumns="True"
ItemsSource="{Binding}"
Name="dataGrid1"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
C# 代码:
namespace InfoSeeker
{
public class InfoWorkData
{
private InfoTableAdapters.InfoWorkTableAdapter infoAdapter;
private Info infoDS;
public InfoWorkData()
{
infoDS = new Info();
infoAdapter = new InfoTableAdapters.InfoWorkTableAdapter();
infoAdapter.Fill(infoDS.InfoWork);
}
public DataView GetInfoWork()
{
return infoDS.InfoWork.DefaultView;
}
}
}
在具有网格的设计器页面位置显示错误:
发生了未处理的异常:
在中键入“MS.Internal.Permissions.UserInitiatedNavigationPermission” 程序集'PresentationFramework,版本=3.0.0.0,文化=中性, PublicKeyToken=31bf3856ad364e35' 未标记为可序列化。在 System.Runtime.Serialization.FormatterServices.InternalGetSerializedMembers(RuntimeType 类型)在 System.Runtime.Serialization.FormatterServices.GetSerializedMembers(类型 类型,StreamingContext 上下文)位于 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象 obj、ISurrogateSelector 代理选择器、StreamingContext 上下文、 SerObjectInfoInit serObjectInfoInit,IFormatterConverter转换器, 对象写入器(objectWriter) ...在:Ms.Internal.Designer.DesignerPane.LoadDesignerView()
编辑:修复了我的 Visual Studio。至少它给了我一个更好的错误消息:
请求类型许可 'System.Data.OleDb.OleDBPermission,system.Data,版本=2.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089' 失败。
I have used the marvelous example posted at:
http://www.codeproject.com/KB/WPF/WPFDataGridExamples.aspx
to bind a WPF datagrid to a datatable.
The source code below compiles fine; it even runs and displays the contents of the InfoWork datatable in the WPF datagrid. Hooray! But the WPF page with the datagrid will not display in the designer. I get an incomprehensible error instead on my design page which is shown at the end of this posting. I assume the designer is having some difficulty instantiating the dataview for display in the grid. How can I fix that?
XAML Code:
xmlns:local="clr-namespace:InfoSeeker"
<Window.Resources>
<ObjectDataProvider
x:Key="InfoWorkData"
ObjectType="{x:Type local:InfoWorkData}" />
<ObjectDataProvider
x:Key="InfoWork"
ObjectInstance="{StaticResource InfoWorkData}"
MethodName="GetInfoWork" />
</Window.Resources>
<my:DataGrid
DataContext="{Binding Source={StaticResource InfoWork}}"
AutoGenerateColumns="True"
ItemsSource="{Binding}"
Name="dataGrid1"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
C# Code:
namespace InfoSeeker
{
public class InfoWorkData
{
private InfoTableAdapters.InfoWorkTableAdapter infoAdapter;
private Info infoDS;
public InfoWorkData()
{
infoDS = new Info();
infoAdapter = new InfoTableAdapters.InfoWorkTableAdapter();
infoAdapter.Fill(infoDS.InfoWork);
}
public DataView GetInfoWork()
{
return infoDS.InfoWork.DefaultView;
}
}
}
Error shown in place of the designer page which has the grid on it:
An unhandled exception has occurred:
Type 'MS.Internal.Permissions.UserInitiatedNavigationPermission' in
Assembly 'PresentationFramework, Version=3.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' is not marked as serializable. at
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType
type) at
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type
type, StreamingContext context) at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
at
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object
obj, ISurrogateSelector surrogateSelector, StreamingContext context,
SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter,
ObjectWriter objectWriter)
...At:Ms.Internal.Designer.DesignerPane.LoadDesignerView()
edit: Repaired my Visual Studio. At least it gives me a better error message:
Request for the permission of type
'System.Data.OleDb.OleDBPermission,system.Data, Version=2.0.0.0,
Culture=neutral,PublicKeyToken=b77a5c561934e089' failed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我正在开发的项目位于(公司)网络驱动器上。如果我将项目移动到本地 C: 驱动器,错误就会消失。
The project I was developing was on a (corporate) network drive. If I move the project to my local C: drive the error goes away.