2016-04-14 26 views
0

我正在尝试创建一个简单的WPF项目,它通过文本框输入学生的详细信息,然后在单击添加学生按钮后将它们存储到学生数据库中。WPF应用程序中的歧义错误

这是工作的罚款,并在数据库中存储没有问题

private void OnAddNewStudent(object sender, RoutedEventArgs e) 
{ 
    using (db1104983Entities1 context = new db1104983Entities1()) //Contained within using so it automatically disposes when it is out of scope 
    { 
     Student student1 = new Student //creates new Student Object 
     { 
      MatricNo= txtM.Text, 
      FirstName = txt1.Text, 
      LastName = txt2.Text, 
      Component1 = txtcom1.Text, 
      Component2 = txtcom2.Text, 
      Component3 = txtcom3.Text, 
     }; 

     MessageBox.Show("Student Added Succesfully"); // Advises user the record has succesfully been added 
     context.Students.Add(student1); //Adds Student object to DB 
     context.SaveChanges(); // Commits change to dDB 

     //Clears all textboxes once record has been added 
     txtM.Clear(); 
     txt1.Clear(); 
     txt2.Clear(); 
     txtcom1.Clear(); 
     txtcom2.Clear(); 
     txtcom3.Clear(); 
    } 
} 

我现在想显示什么是使用数据绑定拖动所需的表,在这种情况下是方法数据库学生。在xaml的网格中。

现在,我已经做了,这样就不会在网格中显示任何东西,也不再要求,并且告诉我,有我的matricno,名字等

看到错误

之间的模糊

Error Message

我只是想检查它是否真的很明显我做错了?

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <TabControl HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517"> 
     <TabItem Header="Input"> 
      <Grid Background="#FFE5E5E5" Margin="0,-1,-14,-7"> 
       <Button Content="Add Student" HorizontalAlignment="Left" Margin="10,252,0,0" VerticalAlignment="Top" Width="94" Height="26" Click="OnAddNewStudent"/> 
       <TextBox x:Name="txtM" HorizontalAlignment="Left" Height="23" Margin="148,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
       <TextBox x:Name="txtcom1" HorizontalAlignment="Left" Height="23" Margin="355,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
       <TextBox x:Name="txt1" HorizontalAlignment="Left" Height="23" Margin="148,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
       <TextBox x:Name="txtcom2" HorizontalAlignment="Left" Height="23" Margin="355,75,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
       <TextBox x:Name="txtcom3" HorizontalAlignment="Left" Height="23" Margin="355,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
       <TextBox x:Name="txt2" HorizontalAlignment="Left" Height="23" Margin="148,128,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 
       <Label Content="First Name:" HorizontalAlignment="Left" Margin="11,75,0,0" VerticalAlignment="Top" Height="23" Width="93"/> 
       <Label Content="Matriculation No:" HorizontalAlignment="Left" Margin="11,22,0,0" VerticalAlignment="Top" Height="23" Width="105"/> 
       <Label Content="Last Name:" HorizontalAlignment="Left" Margin="11,128,0,0" VerticalAlignment="Top" Height="23" Width="93"/> 
       <Label Content="Com 1" HorizontalAlignment="Left" Margin="302,22,0,0" VerticalAlignment="Top"/> 
       <Label Content="Com 2" HorizontalAlignment="Left" Margin="302,72,0,0" VerticalAlignment="Top"/> 
       <Label Content="Com 3" HorizontalAlignment="Left" Margin="302,125,0,0" VerticalAlignment="Top"/> 
       <Button Content="Purge Database" HorizontalAlignment="Left" Margin="130,252,0,0" VerticalAlignment="Top" Width="94" Height="26" Click="OnPurgeDB"/> 
      </Grid> 
     </TabItem> 
     <TabItem Header="TabItem"> 
      <Grid Background="#FFE5E5E5"/> 
     </TabItem> 
    </TabControl> 

</Grid> 
</Window> 
+1

@nmtuan括号使用对象初始化时是可选的。 – CodingGorilla

+0

谢谢,我学到了一些东西:D – rocketspacer

回答

0

你需要在你的XAML文件看

此错误是由两个项目具有相同的名称引起的,删除或重命名其中的一个

<Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="74.778,81.038,0,0" VerticalAlignment="Top"/> 
    <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="83.781,135.057,0,0" VerticalAlignment="Top"/> 
+0

嗨,伙伴,我看不到在我的XAML中有任何重复,它很短。请参阅以下内容:http://i.imgur.com/crLefOp.png?hl=zh-CN – Conkbabes

+0

我认为您需要为您的标签添加他们似乎没有的标签。 – GreatJobBob

+0

我看到了,它抱怨component3被定义了多次,但是我没有在您发布的代码中看到它的定义。您可以搜索component3 Do Control Shift F在整个解决方案中查找文件并搜索Component3 – GreatJobBob

0

因为你的学生类是部分类,正如我在屏幕截图(http://i.imgur.com/crLefOp.png?1)中看到的那样,您可以在包含类另一部分的文件中查找另一个具有相同名称的声明。

0

的问题是,因为当我拖放它已经创建了变量名的另一个文件中的新的DataGrid,删除文件后,解决了该问题

相关问题