我正在尝试使用包含姓名,入学编号,学分等的简单表单为学生注册创建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!");
}
}
答案 0 :(得分:1)
你真的应该用绑定来做这件事,你将Level文本框绑定到学生的Level属性,在你的模型上实现iNotifyPropertyChanged。我建议你研究绑定并以这种方式重新设计它。
但是,如果您希望继续进行当前的设计,我建议您做出改变,以实现您期望的行为:
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"
属性添加到Level Textbox,因为它不应该从界面设置。
<TextBox x:Name="txtLevel" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
4)由于你没有使用绑定,在你的Advance_click中,你需要将返回的值发回给接口:
private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
txtLevel.Text = student.Advance(); //this should be the call of the method
}
答案 1 :(得分:0)
我认为最简单的是省略集合,然后让btnAdvance同时执行。问题是因为这是一个不同的往返,所以学生不会提前存在。
即。高级必须查看文本框并弄清楚。
答案 2 :(得分:0)
您正在更新模型项,之后不会将控件设置为更新后的值。