将 WPF 数据网格绑定到数据表

发布于 2024-08-26 17:54:13 字数 2529 浏览 4 评论 0原文

我使用了发布在以下位置的精彩示例:

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 技术交流群。

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

发布评论

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

评论(1

感情旳空白 2024-09-02 17:54:13

我正在开发的项目位于(公司)网络驱动器上。如果我将项目移动到本地 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.

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