我有两个ListBox
es,Source
和Destination
。目的地ListBox
已由用户选择Items
。如何总结列表框中的项目?
如何汇总目的地项目ListBox
?
目的地ListBox
有两种数据类型String
(说明)和decimal
(成本)。 我只想总结成本。这里是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>
这是我试图总结成本代码:
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;
他们选择Items
他/她需要,我的挑战是获得总的用户选择的项目(已移至ListBox
目的地)
'ItemsSource'从哪里来?张贴相关的代码。 –