【发布时间】:2016-01-27 10:45:58
【问题描述】:
我对使用 xaml 和 C# 在 WPF 中进行编程非常陌生,我在论坛中搜索过类似的问题,但找不到任何解决问题的方法。
我制作了一个包含三个项目的组合框,每个项目都有一个文本内容。选择其中一个项目时,我想将文本框中的值乘以一个数字并在标签中显示结果。这是我的代码:
public MainWindow()
{
InitializeComponent();
int a = Int32.Parse(weight.Text);
double b;
if (this.wcf.SelectedItem==weighing)
{
b = a * 1.03;
wll.Content = b.ToString();
}
else if (this.wcf.SelectedItem == uptodate)
{
b = a * 1.1;
wll.Content = b.ToString();
}
else if (this.wcf.SelectedItem == lessupdated)
{
b = a * 1.2;
wll.Content = b.ToString();
}
}
“weight”是文本框的名称,“wcf”是组合框的名称,“weighing”、“uptodate”和“lessupdated”是组合框项的名称,“wll”是标签的名称.这些在主窗口的 xaml 中定义。
【问题讨论】:
-
在构造函数中读取某些内容没有意义(用户尚未输入/选择某些内容)。您更有可能想要使用一些事件(例如组合框的
SelectionChanged或TextChanged)并在其中执行上述逻辑(解析、检查和设置)。或者是什么问题?