如何在WPF中绑定用户输入
本文关键字:绑定 用户 输入 WPF | 更新日期: 2023-09-27 18:12:39
我正在研究这个应用程序,它获取用户的输入并将它们放入ListBox中,并找到这些数字的总和(一切正常)。我的问题是,每次用户输入一个新数字,该数字显示在窗口中(我不希望那样),而不是我只希望当前总和的所有ListBox数字显示在窗口上。因此,如果用户输入一个新的数字,一个新的总和应该显示在窗口上。请帮帮我。提前谢谢你。这是我的代码运行得很好....
private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
{
int sum = 0;
//Hides InputBox and takes input text from user.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Ensuring that input from user is a integer number
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
//Adding number of coins to CoinListBox
CoinListBox.Items.Add(result);
}
else
{
MessageBox.Show("Please enter a number of coins");
}
sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));
if(sum > 30)
{
//Removing last coin in case number of coins exceeds 30
CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
// Resets InputBox.
InputTextBox.Text = String.Empty;
}
您的主要问题是在方法中声明sum
。
简单地使用:
int sum = 0;
private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
{
if (sum+(int) (InputTextBox.Text) > 30)
{
//Removing last coin in case number of coins exceeds 30
CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
//Hides InputBox and takes input text from user.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Ensuring that input from user is a integer number
string number = InputTextBox.Text;
int num;
if(int.TryParse(number,out num))
{
sum += num;
try { CoinListBox.Items.RemoveAt(0);
} catch
{}
CoinListBox.Items.Add(sum);
}
InputTextBox.Text = string.Empty;
}
古德勒克。
-
Slashy的答案是不完整的,因为它允许添加负数。
-
如果你将InputTextBox的可见性设置为塌陷,那么你是如何获得用户输入的?使用InputTextBox的意义是什么?
-
如果你想只显示sum,那么你可以通过将ListBox的可见性设置为collapse来隐藏它。那么,现在你的代码应该看起来像:
... CoinListBox.Visibility = System.Windows.Visibility.Collapsed; ... sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x)); if (sum > 30) { sum -= result; // removing excess coin //Removing last coin in case number of coins exceeds 30 CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1); MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question); if (answer == MessageBoxResult.Yes) { Application.Current.Shutdown(); } }
-
你的代码的一个更好的版本也应该检查负数,就像:
List<int> coinList = new List<int>(); private void ClickToAddMoreCoins2(object sender, RoutedEventArgs e) { int sum = 0; //Hides InputBox and takes input text from user. //InputTextBox.Visibility = System.Windows.Visibility.Collapsed; // Ensuring that input from user is a integer number String input = InputTextBox.Text; int result = 0; if (int.TryParse(input, out result) && result > 0) { //Adding number of coins to CoinListBox coinList.Add(result); } else { MessageBox.Show("Please enter a valid number of coins"); } sum = coinList.Sum(); if (sum > 30) { sum -= result; //Removing last coin in case number of coins exceeds 30 coinList.RemoveAt(coinList.Count - 1); MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question); if (answer == MessageBoxResult.Yes) { Application.Current.Shutdown(); } } tbSum.Text = "Sum = " + sum.ToString(); // Resets InputBox. InputTextBox.Text = String.Empty; InputTextBox.Focus(); }