这可能是一件小事,但我找不到答案。C#WPF:使用列表框中的按钮查找最大值
我想要的是当你添加一些数字到列表框中,当你按下“最大按钮”时,它将显示列表中最大的数字。
这里是如何看起来像:Image Link
这是代码。
private void addButton_Click(object sender, RoutedEventArgs e)
{
ListBoxItem newItem = new ListBoxItem();
newItem.Content = addNumberTextBox.Text;
numberListBox.Items.Add(newItem);
}
private void sumButton_Click(object sender, RoutedEventArgs e)
{
int sum = 0;
foreach (ListBoxItem item in numberListBox.Items)
{
sum += Convert.ToInt32(item.Content);
}
sumTextBox.Text = Convert.ToString(sum);
}
private void largestButton_Click(object sender, RoutedEventArgs e)
{
????
}
}
的可能的复制[如何查找列表框的最大和最小值] (http://stackoverflow.com/questions/7963144/how-to-find-the-greatest-and-lowest-value-in-listbox) – abdul
而不是明确添加ListBoxIte ms将数字字符串作为内容,您应该将您的ListBox绑定到视图模型中的'ObservableCollection'。数值运算(如Sum和Largest)应该作为视图模型中的ObservableCollection操作的方法来实现。在网络上搜索MVVM。 –
Clemens
另请参阅MSDN上的[数据模板概述](https://msdn.microsoft.com/en-us/library/ms742521(v = vs.110).aspx)文章。 – Clemens