简体   繁体   English

WPF中自动生成的代码显示错误。 (C#,WPF,Visual Studio社区2013)

[英]Auto-generated code in WPF shows error. (C#, WPF, Visual Studio Community 2013)

Recently i have started playing with C# in Visual Studio 2013 Community. 最近,我开始在Visual Studio 2013社区中使用C#。 I was creating a simple data binding while i encountered following error. 我遇到以下错误时正在创建一个简单的数据绑定。 What i was doing i was using drag-n-drop fr om my database to main window. 我在做什么,我使用的是从数据库拖放到主窗口。 Visual studio generated required XAML and .cs file. Visual Studio生成了必需的XAML和.cs文件。 There was following error present with the code. 该代码存在以下错误。 I searched a lot and didn't found a solution. 我搜索了很多,却没有找到解决方案。 It was showing on 它显示在

Error: "an object reference is required to access non-static field method or property" 错误:“访问非静态字段方法或属性需要对象引用”

following is my XAML. 以下是我的XAML。

 <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:rrplproj" x:Class="rrplproj.MainWindow"
        Title="MainWindow" Height="700" Width="1024" WindowState="Maximized" Loaded="Window_Loaded">
    <Window.Resources>
        <local:rDataSet x:Key="rDataSet"/>
        <CollectionViewSource x:Key="dateViewSource" Source="{Binding Date, Source={StaticResource rDataSet}}"/>
        <CollectionViewSource x:Key="deliveryViewSource" Source="{Binding Delivery, 
Source={StaticResource rDataSet}}"/>

    </Window.Resources>

    <Grid Background="#FFDBFDF8">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="165*"/>
            <ColumnDefinition Width="172*"/>
            <ColumnDefinition Width="171*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="56*"/>
            <RowDefinition Height="149*"/>
            <RowDefinition Height="18*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="grid1" DataContext="{StaticResource dateViewSource}" HorizontalAlignment="Left" 
Margin="19,23,0,0" Grid.Row="1" VerticalAlignment="Top" Height="32" Width="151">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Date:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" 
VerticalAlignment="Center"/>
            <DatePicker x:Name="dateDatePicker" Grid.Column="1" HorizontalAlignment="Left" Margin="3" Grid.Row="0" 
SelectedDate="{Binding Date, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" 
VerticalAlignment="Center"/>
        </Grid>
        <Grid x:Name="grid2" Grid.Column="1" DataContext="{StaticResource deliveryViewSource}" 
HorizontalAlignment="Left" Margin="64,18,0,0" Grid.Row="1" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Truck No:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" 
VerticalAlignment="Center"/>
            <TextBox x:Name="truckNoTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" 
Grid.Row="0" Text="{Binding TruckNo, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" 
VerticalAlignment="Center" Width="120"/>
        </Grid>
    </Grid>

and following is auto generated code by ide in C#. 以下是在C#中由ide自动生成的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace rrplproj
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            // Load data into the table Date. You can modify this code as needed.
            rrplproj.rDataSetTableAdapters.DateTableAdapter rDataSetDateTableAdapter = 
        new rrplproj.rDataSetTableAdapters.DateTableAdapter();
            rDataSetDateTableAdapter.Fill(rDataSet.Date);
            System.Windows.Data.CollectionViewSource dateViewSource = 
            ((System.Windows.Data.CollectionViewSource)(this.FindResource("dateViewSource")));
            dateViewSource.View.MoveCurrentToFirst();
            // Load data into the table Delivery. You can modify this code as needed.
            rrplproj.rDataSetTableAdapters.DeliveryTableAdapter rDataSetDeliveryTableAdapter = 
            new rrplproj.rDataSetTableAdapters.DeliveryTableAdapter();
            rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);
            System.Windows.Data.CollectionViewSource deliveryViewSource = 
            ((System.Windows.Data.CollectionViewSource)(this.FindResource("deliveryViewSource")));
            deliveryViewSource.View.MoveCurrentToFirst();
        }
    }
}

The error is shown here 错误显示在这里

rDataSetDateTableAdapter.Fill(rDataSet.Date);

and here: 和这里:

rDataSetDeliveryTableAdapter.Fill(rDataSet.Delivery);

it is showing red line under 它在下面显示红线

(rDataSet.Date)

and

(rDataSet.Delivery)

Your help is appreciated. 感谢您的帮助。

Thank you for reading till the last line. 感谢您阅读到最后一行。

Never tried it with data set, but it simple means that it can not create an instance of rDataSet which is in this namespace - rrplproj we would need a little info abt rDataSet in order to understand it further .. also read about creating dataset instances in xaml.. 从来没有尝试过使用数据集,但是它很简单,就意味着它无法创建此命名空间中的rDataSet实例-rrplproj,我们将需要一些信息abt rDataSet以便进一步了解它。 xaml ..

or else in the constructor of your code behind write this ... 否则在后面的代码的构造函数中写这个...

myObj = new rDataSet();
this.Resources.Add("rDataSet", myObj);

and remove this from your xaml 并将其从您的xaml中删除

<local:rDataSet x:Key="rDataSet"/>

this would add a resource to rDataSet 这将为rDataSet添加资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM