2013-10-08 28 views
0

我是C#/ Visual Studio的初學者,需要在這裏向正確的方向推動。通過驗證將文本字符串轉換爲屬性0

我有一個主Window.xaml.cs文件,其中我有一個TextBox可以接收名字,還有兩個按鈕「Set」來更新一個名爲Student的類,該類包含一個私有屬性和一個「清除」按鈕,它工作正常。

我無法弄清楚如何從文本框中將我的字符串放到我的Student類中,並且在添加更多屬性之前需要獲取此字符串。

任何幫助將受到感謝。

主窗口代碼如下

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void btnSet_Click(object sender, RoutedEventArgs e) 
    { 

    } 

    private void btnClear_Click(object sender, RoutedEventArgs e) 
    { 
     txtFirstName.Clear(); 

    } 

我的學生類代碼如下

namespace StudentRecordsSystem 
{ 
public class Student 
{ 
    private string FirstName 
    { 
     get 
     { 
      throw new System.NotImplementedException(); 
     } 
     set 
     { 
      // check that string is not empty 

      if (string.IsNullOrEmpty(value) == true) 
       throw new ArgumentOutOfRangeException("Please enter a first name"); 
     } 
    } 
+2

嘿,你最好在這裏發帖之前得到一些教程。這不是你如何實現一個類,這只是其中一個不正確的事情。我不想粗魯,但先查看一下:https://www.google.com.br/search?q=C%23+tutorial – Tico

+0

Tico是對的,但這沒有幫助 - 我明白了。查找可見性(私有,公共),屬性和字段變量以及對象生命週期。 – Kobor42

+0

由於您使用的是WPF,我建議先以適當的WPF方式開始。看看我的答案。一旦你習慣了,它就容易多了。 – Bijan

回答

1

你應該你的名字屬性可能設置爲公開,就沒有必要把它變成私有。

您需要實例化您的學生類,然後將該文本框的值分配給該屬性。

private Student s; 
public MainWindow() 
{ 
    InitializeComponent(); 
    s = new Student() 
} 
private void btnSet_Click(object sender, RoutedEventArgs e) { 
    s.FirstName = txtFirstName.Text; 
} 

如果您發現無法從表單代碼訪問Student類,請檢查您的名稱空間。

0

如果你想你可以做一個構造爲學生類,像這樣:

public Student(string firstName) 
{ 
    FirstName = firstName; 
} 

這將允許你做這樣的事情:

學生S =新的學生(「喬希「);

請注意,此方法聲明中的firstName具有小寫字母f,而變量(至少在我的用法中)具有大寫字母F.這不是常規的,我相信約定是使用camelcase,但要在私有變量前加下劃線(_)。這主要是一個C++約定,但是由於C#被設計爲與C++類似,所以它適合這項工作。

0

試試這個:

在xaml.cs:

通知DataContext的是如何創建和使用檢索數據。

public MainWindow() 
{ 
    InitializeComponent(); 
    DataContext = new Student(); 
} 
private void Clear_Clicked(object sender, RoutedEventArgs e) 
{ 
    ((Student)DataContext).FirstName = string.Empty; 
} 

這個類添加到同一項目的主窗口:

使用有效性規則的一點是,避免惱人的消息框,而是標記其控制當規則被違反。

public class NamesValidationRule : ValidationRule 
{ 
    public string MinimumLetters { get; set; } 

    public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
    { 
     var str = value as string; 
     if(str == null) 
      return new ValidationResult(false, "Please enter first name"); 
     if(str.Length < Convert.ToInt32(MinimumLetters)) 
      return new ValidationResult(false, string.Format("Minimum Letters should be {0}", MinimumLetters)); 
     return new ValidationResult(true, null);  
    } 
} 
在XAML

添加的xmlns ......到第一個XAML的,然後用結合自/至姓讀/寫數據。還要注意添加到綁定的驗證規則。

注意:除非手動更改,否則MainWindow中的所有綁定均相對於其DataContext進行尋址。 (當你寫{Binding FirstName}這意味着DataContext.FirstName

<MainWindow xmlns:local="clr-namespace:MyNamespace.Project1" 
     ...> 
    ... 
    <TextBox> 
     <TextBox.Text> 
      <Binding Path="FirstName" UpdateSourceTrigger="PropertyChanged"> 
       <Binding.ValidationRules> 
        <local:NamesValidationRule MinimumLetters="3" /> 
       </Binding.ValidationRules> 
      </Binding> 
     </TextBox.Text> 
    </TextBox> 
    ... 
    <Button Content="Clear" Click="Clear_Clicked"/> 
    ... 
</MainWindow> 
在StudentRecordsSystem

注意:不要忘了使用的DependencyProperty,而不是正常的財產,否則綁定將無法正常工作。不要忘了自DependencyObject

派生類(學生)
using System.Windows; 
... 
public class Student : DependencyObject 
{ 
    //FirstName Dependency Property 
    public string FirstName 
    { 
     get { return (string)GetValue(FirstNameProperty); } 
     set { SetValue(FirstNameProperty, value); } 
    } 
    public static readonly DependencyProperty FirstNameProperty = 
     DependencyProperty.Register("FirstName", typeof(string), typeof(Student), new UIPropertyMetadata(null)); 
    } 

,你會不會需要的「設置」按鈕,再

0

主窗口應該是這樣的:

public partial class MainWindow : Window 
{ 
    private Student myOnlyStudent; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     myOnlyStudent = new Student() 
    } 

    private void btnSet_Click(object sender, RoutedEventArgs e) 
    { 
     myOnlyStudent.FirstName = txtFirstName.Text; 
    } 

    private void btnClear_Click(object sender, RoutedEventArgs e) 
    { 
     txtFirstName.Clear(); 
    } 
} 

如果你堅持使用屬性拋出異常,那麼學生類必須是這樣的:

public class Student 
{ 
    private string mFirstName; 
    public string FirstName 
    { 
     get 
     { 
      return mFirstName; 
     } 
     set 
     { 
      if (string.IsNullOrEmpty(value) == true) 
       throw new ArgumentOutOfRangeException("Please enter a first name"); 
      mFirstName = value; 
     } 
    } 
} 

但是在你的等級首發我會建議你使用公共代替私人和跳躍屬性(都是知名度和屬性複雜):

public class Student { public string FirstName; } 

public partial class MainWindow : Window 
{ 
    private void btnSet_Click(object sender, RoutedEventArgs e) 
    { 
     if (string.isNullOrEmpty(txtFirstName.Text)) 
      throw new Exception("Please type first name!"); 
     myOnlyStudent.FirstName = txtFirstName.Text; 
    } 
}