计算ListBoxes中两个值的总和?

时间:2016-10-27 14:05:30

标签: c# wpf xaml listbox

我在WPF应用程序中有两个ListBoxes,包含学校课程和费用。但是,在运行时,我应该显示总共两个值的标签仅显示最近选择的值。如何才能使总计实际计算出两个选定值?

XAML:

<Grid>
    <ListBox x:Name="courseListBox" SelectionChanged="courseListBox_SelectionChanged" HorizontalAlignment="Left" Height="126" VerticalAlignment="Top" Width="120" Margin="42,35,0,0">
        <ListBoxItem>Intro to Comp Sci</ListBoxItem>
        <ListBoxItem>Honors Comp Sci</ListBoxItem>
        <ListBoxItem>AP Comp Sci A</ListBoxItem>
        <ListBoxItem>AP Comp Sci P</ListBoxItem>
        <ListBoxItem>Independent Study</ListBoxItem>
        <ListBoxItem>Webpage Design</ListBoxItem>
    </ListBox>

    <ListBox x:Name="classListBox" SelectionChanged="classListBox_SelectionChanged" HorizontalAlignment="Left" Height="85" Margin="42,197,0,0" VerticalAlignment="Top" Width="120">
        <ListBoxItem>Freshman</ListBoxItem>
        <ListBoxItem>Sophomore</ListBoxItem>
        <ListBoxItem>Junior</ListBoxItem>
        <ListBoxItem>Senior</ListBoxItem>
    </ListBox>
    <Label x:Name="totalLabel" Content="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="168,34,0,0"/>
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="120" Margin="42,9,0,0"/>
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="120" Margin="42,169,0,0"/>

</Grid>

C#:

public partial class MainWindow : Window
{
    public int courseFee = 0;
    public int classFee = 0;
    public int totalCost = 0;
    public MainWindow()
    {
        InitializeComponent();
    }
    private void courseListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selected = courseListBox.SelectedIndex;
        if (selected == 0)
            classFee = 545;
        if (selected == 1)
            classFee = 615;
        if (selected == 2)
            classFee = 1500;
        if (selected == 3)
            classFee = 1000;
        if (selected == 4)
            classFee = 2500;
        if (selected == 5)
            classFee = 1720;
        totalCost = courseFee + classFee;
        totalLabel.Content = totalCost;
    }

    private void classListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selected = classListBox.SelectedIndex;
        if (selected == 0)
            classFee = 350;
        if (selected == 1)
            classFee = 275;
        if (selected == 2)
            classFee = 200;
        if (selected == 3)
            classFee = 150;
        totalCost = courseFee + classFee;
        totalLabel.Content = totalCost;
    }
}

3 个答案:

答案 0 :(得分:0)

服务器是无状态的,所以你不记得回发之间的费用价值。只需将两个程序结合起来。

protected void getTotal() 
    {
        int selected = courseListBox.SelectedIndex;
        if (selected == 0)
            classFee = 545;
        if (selected == 1)
            classFee = 615;
        if (selected == 2)
            classFee = 1500;
        if (selected == 3)
            classFee = 1000;
        if (selected == 4)
            classFee = 2500;
        if (selected == 5)
            classFee = 1720;
        int total = classFee;
        selected = classListBox.SelectedIndex;
        if (selected == 0)
            classFee = 350;
        if (selected == 1)
            classFee = 275;
        if (selected == 2)
            classFee = 200;
        if (selected == 3)
            classFee = 150;
        total += classFee;
        totalLabel.Content = total;
    }

    private void classListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
       getTotal();
    }

(使两个列表框使用相同的事件处理程序)

答案 1 :(得分:0)

好好开始将courseListBox_SelectionChanged方法中的classFee更改为courseFree

代码可能是这样的......

int value = 0;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                value += int.Parse(listView1.Items[i].SubItems[e.Column].Text);
            }

            textBox1.Text = value.ToString();

答案 2 :(得分:0)

我认为你的课程费用而不是课程列表中的classFee

.iconTimes:before {
      display: block;
      font-family: FontAwesome;
      content: "\f00d"; 
 }