Recently i have started playing with C# in Visual Studio 2013 Community. 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. There was following error present with the code. I searched a lot and didn't found a solution. It was showing on
最近我开始在Visual Studio 2013社区使用c#。当我遇到以下错误时,我正在创建一个简单的数据绑定。我所做的就是将数据库拖拽到主窗口。Visual studio生成所需的XAML和.cs文件。代码中出现了以下错误。我搜索了很多,没有找到解决办法。这是显示在
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.
谢谢你读完最后一行。
1 个解决方案
#1
0
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添加一个资源。
#1
0
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添加一个资源。