I have two ListBox
es, Source
and Destination
. The destination ListBox
has Items
selected by the user.
我有两个ListBox,Source和Destination。目标ListBox具有用户选择的项目。
How do I sum items in a destination ListBox
?
如何汇总目的地ListBox中的项目?
The Destination ListBox
has two data types String
(Description) and decimal
(Cost). I want to sum up only the Cost. Here is the XAML Code
Destination ListBox有两种数据类型String(Description)和decimal(Cost)。我只想总结成本。这是XAML代码
<ListBox Height="237" HorizontalAlignment="Left" Margin="44,191,0,0" Name="lstDestination" VerticalAlignment="Top" Width="264" Grid.ColumnSpan="2" ItemsSource="{Binding Source={StaticResource myItemList}}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<DockPanel >
<TextBlock FontWeight="Bold" Text="Item:" DockPanel.Dock="Left" Margin="5,0,10,0"/>
<TextBlock Text="{Binding Path=Resource}" Foreground="Green" FontWeight="Bold" />
<TextBlock FontWeight="Bold" Text="Cost:" DockPanel.Dock="Left" Margin="5,0,10,0"/>
<TextBlock Text="{Binding Path=Cost}" FontWeight="Bold" />
</DockPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is the code I tried to sum up Cost:
这是我试图总结费用的代码:
private decimal SumDLstBox()
{
decimal totalCost;
foreach (var i in lstDestination.SelectedItems)
{
totalCost+=i.Cost; //Error is thrown here
}
return totalCost;
}
//Data Source to List Box Source
var items = from r in dc.IResources select r;
lstSource.ItemsSource = items;
The user them selects the Items
s/he needs, my challenge is to get the total of the selected items(Which have been moved to the ListBox
Destination)
用户他们选择他/她需要的项目,我的挑战是获取所选项目的总数(已移动到ListBox目的地)
3 个解决方案
#1
1
To operate on your bound data, you should cast it to a list of the datatype you're working with. For eg.
要对绑定数据进行操作,您应该将其强制转换为您正在使用的数据类型列表。例如。
private decimal SumDLstBox()
{
return lstDestination.SelectedItems.Cast<Foo>().Sum(f => f.Cost);
}
Where Foo
is the datatype of the List you bound to the control.
其中Foo是绑定到控件的List的数据类型。
#2
0
Your .cs
-file will know the property lstDestination
. You can use a foreach
loop to iterate through all elements in the lstDestination
. In that same loop, you can add up the costing.
您的.cs文件将知道属性lstDestination。您可以使用foreach循环遍历lstDestination中的所有元素。在同一个循环中,您可以累计成本核算。
#3
0
Seems to me that you don't to iterate by lstDestination.SelectedItems but by the lstDestination.Items
在我看来,你不要通过lstDestination.SelectedItems迭代,而是通过lstDestination.Items迭代
#1
1
To operate on your bound data, you should cast it to a list of the datatype you're working with. For eg.
要对绑定数据进行操作,您应该将其强制转换为您正在使用的数据类型列表。例如。
private decimal SumDLstBox()
{
return lstDestination.SelectedItems.Cast<Foo>().Sum(f => f.Cost);
}
Where Foo
is the datatype of the List you bound to the control.
其中Foo是绑定到控件的List的数据类型。
#2
0
Your .cs
-file will know the property lstDestination
. You can use a foreach
loop to iterate through all elements in the lstDestination
. In that same loop, you can add up the costing.
您的.cs文件将知道属性lstDestination。您可以使用foreach循环遍历lstDestination中的所有元素。在同一个循环中,您可以累计成本核算。
#3
0
Seems to me that you don't to iterate by lstDestination.SelectedItems but by the lstDestination.Items
在我看来,你不要通过lstDestination.SelectedItems迭代,而是通过lstDestination.Items迭代