我一直試圖停止添加數字,一旦它達到30或超過30(最大容量)。我的代碼運行並添加數字就好了。我的問題是如何從獲得更多的數字停止一旦它擊中30或進入超過30如何停止向WPF中的ListBox添加元素?
private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
{
//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;
var result = 0;
if (int.TryParse(input, out result))
{
//Adding number of coins to CoinListBox
//CoinListBox.Items.Add(result);
sum += result;
try
{
CoinListBox.Items.RemoveAt(0);
}
catch
{ }
CoinListBox.Items.Add(sum);
}
else
{
MessageBox.Show("Please enter a number of coins");
}
//sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));
if(sum > 30)
{
//CoinListBox.Items.Add
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;
}
//This method hides InputBox.
}
'將'ListBox'綁定到像List <>的集合,並將集合限制爲您的需要。只需驗證你的清單。 –
@AbinMathew怎麼樣?我真的很新的WPF:(... – progx
找到這[鏈接](http://stackoverflow.com/q/9391746/2470362) –