我正在尝试为学生注册创建一个 WPF 应用程序,其中包含一个包含姓名、入学编号、学分等的简单表格。
我有一个 Mainwindow.xaml
和一个 Student.cs
。
在我的 MainWindow.xaml
中,我有一个高级按钮,可以根据学分提高学生的水平(如果学生的学分超过 120,则水平应提高到“2 ")
这是带有 Advance() 方法的 Student.cs
class Student
{
private int matric;
private int level;
private int credits;
public Student() { }
public int Matric
{
get { return matric; }
set
{
//there should be a range check for the
matric = value;
}
}
public int Level
{
get { return level; }
set { level = value; }
}
public int Credits
{
get { return credits; }
set { credits = value; }
}
//this is my problem:
public int Advance()
{
if (Credits >= 0 && Credits < 120)
{
return Level;
}
else if (credits >= 120)
{
return Level++;
}
else if (credits >= 240)
{
return Level++;
}
else if (Credits >= 360)
{
return Level++;
}
else
{
return 0;
}
}
}
MainWindow.xaml,只有按钮和文本框的部分
<TextBox x:Name="txtLevel" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtCredit" HorizontalAlignment="Left" Height="23" Margin="184,328,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnAdvance" Content="Advance" HorizontalAlignment="Left" Margin="324,267,0,0" VerticalAlignment="Top" Width="75" Click="btnAdvance_Click"/>
以及我尝试调用该方法的位置 MainWindow.xaml.cs
public partial class MainWindow : Window
{
Student student;
public MainWindow()
{
InitializeComponent();
}
private void btnSet_Click(object sender, RoutedEventArgs e)
{
student = new Student();
student.Credits = int.Parse(txtCredit.Text);
student.Level = int.Parse(txtLevel.Text);
}
private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
student.Advance(); //this should be the call of the method
}
}
当然不行... 谁能帮帮我?
编辑 这就是我现在拥有的,仍然无法正常工作
public void Advance()
{
if (Credits >= 0 && Credits < 120)
{
Level = 1;
}
else if (credits >= 120 && Level == 1)
{
Level = 2;
}
else if (credits >= 240 && Level == 2)
{
Level = 3;
}
else if (Credits >= 360 && Level == 3)
{
Level = 4;
}
else if (Level == 4)
{
MessageBox.Show("You can't advance more!");
}
else
{
MessageBox.Show("Advance is not possible!");
}
}
最佳答案
实际上,您应该使用绑定(bind)来执行此操作,将 Level 文本框绑定(bind)到学生的 Level 属性,在您的模型上实现 iNotifyPropertyChanged。我建议您研究绑定(bind)并以这种方式重新设计它。
但是,如果您希望继续当前的设计,我建议您进行以下更改以实现您期望的行为:
1) 在 btnSet_Click
中,删除此行:student.Level = int.Parse(txtLevel.Text);
您的级别不应由 TextBox 设置;它应该由 Advance 方法设置。
2) 您的 Advance 方法应如下所示:
public int Advance()
{
if (Credits >= 0 && Credits < 120)
{
level = 1;
}
else if (credits >= 120 && credits < 240)
{
level = 2;
}
else if (credits >= 240 && credits < 360)
{
level = 3;
}
else if (Credits >= 360)
{
if (level != 4)
{
level = 4;
}
else
{
MessageBox.Show("You can't advance more!");
}
}
else
{
MessageBox.Show("Advance is not possible!");
}
return level;
}
3) 将 IsReadOnly="True"
属性添加到关卡文本框,因为它不应从界面中设置。
<TextBox x:Name="txtLevel" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
4) 由于您没有使用绑定(bind),因此在您的 Advance_click 中,您需要将返回值发送回界面:
private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
txtLevel.Text = student.Advance(); //this should be the call of the method
}
关于c# - 从 MainWindow 访问类中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995150/