2016-07-22 57 views
0

對於C#和編程,我還是比較陌生,所以我不確定我是否能夠正確地提出這個問題,但這裏就是這樣。我目前正在爲使用Visual Studio的C#專門開發一個類,並且我們給出的任務是使用老師提供的WPF應用程序,然後創建一個.cs文件來輸入我們的代碼以使應用程序正常運行。我在創建控制檯應用程序以及僅使用VB的WPF應用程序方面取得了成功,但我並不完全確定如何使這兩個概念相互協作。使用.cs的WPF應用程序

到目前爲止,我的代碼如下:

MainWindow.xaml(教師提供):

<Window x:Class="CreateClassesObjs.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:CreateClassesObjs" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> 
<Grid> 
    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="66,37,0,0" VerticalAlignment="Top" Width="164" IsDropDownOpen="True"/> 
    <Button x:Name="button" Content="Select this course" HorizontalAlignment="Left" Margin="283,39,0,0" VerticalAlignment="Top" Width="166" Click="button_Click"/> 
    <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="69" Margin="66,233,0,0" VerticalAlignment="Top" Width="164"/> 
    <Label x:Name="label" Content="Please select a course " HorizontalAlignment="Left" Margin="66,7,0,0" VerticalAlignment="Top" Width="383"/> 
    <Label x:Name="label1" Content="You have selected these courses:" HorizontalAlignment="Left" Margin="66,202,0,0" VerticalAlignment="Top" Width="176"/> 

</Grid> 

MainWindow.xaml.cs(教師提供):

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
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 CreateClassesObjs 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 

    Course choice; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     Course course1 = new Course(); 
     Course course2 = new Course(); 
     Course course3 = new Course(); 
     Course course4 = new Course(); 
     Course course5 = new Course(); 
     Course course6 = new Course(); 
     Course course7 = new Course(); 

     course1.setName("IT 145"); 
     course2.setName("IT 200"); 
     course3.setName("IT 201"); 
     course4.setName("IT 270"); 
     course5.setName("IT 315"); 
     course6.setName("IT 328"); 
     course7.setName("IT 330"); 



     this.comboBox.Items.Add(course1); 
     this.comboBox.Items.Add(course2); 
     this.comboBox.Items.Add(course3); 
     this.comboBox.Items.Add(course4); 
     this.comboBox.Items.Add(course5); 
     this.comboBox.Items.Add(course6); 
     this.comboBox.Items.Add(course7); 
    } 

    private void button_Click(object sender, RoutedEventArgs e) 
    { 
     choice = (Course)(this.comboBox.SelectedItem); 
     this.listBox.Items.Add(choice); 
    } 

} 
} 

And Course.cs(我已經開始編寫的代碼):

#region Using directives 
using System; 
using System.Collections.Generic; 
using System.Text; 
#endregion 
namespace CreateClassesObjs 
{ 
public partial class Course :MainWindow 
{ 
    //Field 
    private string courseName; 

    //Method to set courseName to string value 
    public void setName(string newName) 
    { 
     courseName = newName; 
    } 

    //overrides string ToString 
    /*public override string ToString() 
    { 
     // this method returns the name field 
     Course course1 = new Course(); 
     Console.WriteLine(course1.courseName); 

    }*/ 
} 
} 

我一直在試圖從我可以找到的教程中找到它,但我覺得我只是混淆了它。我不是在尋找完整的答案,而是朝正確的方向推動。先謝謝你!

+3

你的問題是什麼? – SLaks

+2

爲什麼'Course'繼承'MainWindow'? – SLaks

+1

我希望這個課程不是爲了教WPF的任何人......教官的代碼只是讓我死在內部。 – Guttsy

回答

0

你真的很接近這個工作。你只需要改變你的ToString方法。我猜你現在看到的是這樣的:

example

如果您沒有看到這個會,因爲你不能編譯,我建議你做一個新的cs文件爲您的課程對象,而不是將它嵌入你的MainWindow課程中。有無理由partial類在這裏。

WPF目前不知道如何在ComboBox中顯示課程。在正常的 WPF應用程序中,可以定義一個DataTemplate來告訴ComboBox如何呈現課程。感謝你的老師所創造的憎惡,我們沒有那種奢侈。相反,你需要在你的ToString方法中返回一些東西來安撫它。我會讓你弄清楚是什麼。

(請注意:這是一個如何不該做WPF,甚至在入門級的一個最好的例子不是你用任何方法故障)

0

我希望這可以幫助,但這裏是我如何能讓程序起作用。你需要定義你的getters和setters,並且你的新課程打印出一些東西。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
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 CreateClassesObjs 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 

     Course choice; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 
      Course course1 = new Course("IT 145"); 
      Course course2 = new Course("IT 200"); 
      Course course3 = new Course("IT 201"); 
      Course course4 = new Course("IT 270"); 
      Course course5 = new Course("IT 315"); 
      Course course6 = new Course("IT 328"); 
      Course course7 = new Course("IT 330"); 

      course1.setName("IT 145"); 
      course2.setName("IT 200"); 
      course3.setName("IT 201"); 
      course4.setName("IT 270"); 
      course5.setName("IT 315"); 
      course6.setName("IT 328"); 
      course7.setName("IT 330"); 

      this.comboBox.Items.Add(course1); 
      this.comboBox.Items.Add(course2); 
      this.comboBox.Items.Add(course3); 
      this.comboBox.Items.Add(course4); 
      this.comboBox.Items.Add(course5); 
      this.comboBox.Items.Add(course6); 
      this.comboBox.Items.Add(course7); 
     } 

     private void button_Click(object sender, RoutedEventArgs e) 
     { 
      choice = (Course)(this.comboBox.SelectedItem); 
      this.listBox.Items.Add(choice); 
     } 

     class Course 
     { 
      private string name = ""; 


      public Course(string name) 
      { 
       this.name = name; 
      } 

      public void setName(string name) 
      { 
       this.name = name; 
      } 

      public string getName() 
      { 
       return name; 
      } 

      public override string ToString() 
      { 
       return getName(); 
      } 
     } 
    } 
}