2014-12-10 207 views
0

我想添加列Job列中的值,但它一直到0.爲什麼會發生這種情況?添加列表視圖列來到0

enter image description here

MainWindow.xaml.cs:

int jobSum = 0; 
int bidSum = 0; 
foreach (dynamic item in PlotListView.Items) 
{ 
    jobSum += Convert.ToInt32(item.Jobs); 
    bidSum += Convert.ToInt32(item.Bids); 
} 
JobBidRatioTextBlock.Text = jobSum.ToString(); 

MainWindow.xaml:約後臺代碼

<ListView x:Name="PlotListView" HorizontalAlignment="Left" Height="228" Margin="5,37,0,0" VerticalAlignment="Top" Width="424"> 
    <ListView.View> 
    <GridView AllowsColumnReorder="False"> 
    <GridViewColumn Width="99" Header="Plot" DisplayMemberBinding="{Binding PlotId}"/> 
    <GridViewColumn Width="99" Header="Area" DisplayMemberBinding="{Binding Area}"/> 
    <GridViewColumn Width="99" Header="Jobs" DisplayMemberBinding="{Binding Jobs}"/> 
    <GridViewColumn Width="99" Header="Bids" DisplayMemberBinding="{Binding Bids}"/> 
    </GridView> 
    </ListView.View> 
</ListView> 

詳細的運行:http://pastebin.com/z5kpDcUM

+0

什麼是運行代碼的代碼隱藏?爲什麼沒有這個領域的約束? – BradleyDotNET 2014-12-10 23:13:24

+0

@BradleyDotNET - 看看http://pastebin.com/z5kpDcUM – methuselah 2014-12-10 23:28:03

+0

不相關:而不是試圖實現自己的關係/對象映射,只是使用實體框架。那就是說,你爲什麼使用動態而不是適當的對象類型?如果你的列包含數字數據,爲什麼你將它映射到string類型的屬性而不是int? – 2014-12-10 23:31:26

回答

1

在你的構造函數主窗口下方的InitializeComponent()這樣寫:

 InitializeComponent(); 
     DependencyPropertyDescriptor 
.FromProperty(ListView.ItemsSourceProperty, typeof(ListView)) 
.AddValueChanged(PlotListView, PlotListView_ItemsSourceChanged); 

一個funtion然後添加到主窗口類:

 private void PlotListView_ItemsSourceChanged(object sender, EventArgs e) 
    { 
     ////Write your summation code here 
    }