添加listview列到0

时间:2014-12-10 23:10:55

标签: c# wpf xaml

我正在尝试添加Job列中的值,但它一直是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

运行的后台代码的详细信息

1 个答案:

答案 0 :(得分:1)

在MainWindow的构造函数中,只需在InitializeComponent()下面写下:

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

然后为MainWindow类添加一个功能:

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