2013-10-08 32 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; 
    } 
}